Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Part isn't moving on the direction I intended. Is there something wrong with my CFrame?

Asked by
Rheines 661 Moderation Voter
5 years ago

Hello, I'm trying to make a laser that spawns 5 studs in front of the character that spans from the sky until the ground. In addition, I would like to move the laser forwards for a few seconds in the direction my character is facing until it gets destroyed.

Currently, my laser spawns on the intended location, but it will always 'look' towards the middle of the baseplate/origin no matter where it was spawned. In addition, the laser will move towards that point in the baseplate instead of the front of my character.

I'm thinking it is because of the CFrame properties that I used, or I'm doing the wrong calculations. Here is my code:

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character.Humanoid
local Character = Player.Character
local Human = Player.Character.Humanoid

local OnCooldown = false

function LaserCutter()
    if OnCooldown == false then
        --load and play animations here
        OnCooldown = true

        --offset by 5 studs in front of the character from high above
        local SkyPosition = Character.HumanoidRootPart.CFrame*CFrame.new(Vector3.new(0,500,-5))
        --raycast downwards
        local Raycast = Ray.new(SkyPosition.p, -SkyPosition.upVector * 800)
        local Ground, GroundPosition = workspace:FindPartOnRay(Raycast, Player.Character, false, false)

        if Ground then print('ground found') end

        --this creates a part up in the sky
        local Origin = Instance.new("Part", workspace)
        Origin.Transparency = 1
        Origin.Anchored = true
        Origin.CanCollide = false
        Origin.CFrame = CFrame.new(SkyPosition.p, Character.Head.CFrame.lookVector)

        --this creates a part on the ground
        local Laser = Instance.new("Part", workspace)
        Laser.BrickColor = BrickColor.new("Deep orange")
        Laser.Size = Vector3.new(3,3,3)
        Laser.Anchored = true
        Laser.Material = "Neon"
        Laser.CFrame = CFrame.new(GroundPosition, Character.Head.CFrame.lookVector)
        Laser.CanCollide = false

        --create a beam connecting the sky and the ground for laser effects
        --[[
        local AttachmentOri = Instance.new("Attachment", Origin)
        local AttachmentEnd = Instance.new("Attachment", Laser)
        local LaserBeam = Instance.new("Beam", Origin)
        LaserBeam.Attachment0 = AttachmentOri
        LaserBeam.Attachment1 = AttachmentEnd
        LaserBeam.FaceCamera = true
        LaserBeam.Segments = 1
        LaserBeam.Width0 = 3
        LaserBeam.Width1 = 3
        LaserBeam.Texture = "rbxassetid://908765959"
        LaserBeam.TextureSpeed = 0.2
        LaserBeam.TextureMode = "Stretch"
        LaserBeam.LightEmission = 0.4
        LaserBeam.LightInfluence = 1]]


        --code for moving the part is here
        local start = 0
        while wait(0.01) do
            start = start + 1
            if start == 100 then
                Origin:Destroy()
                Laser:Destroy()
                break
            end
            Origin.CFrame = Origin.CFrame + Origin.CFrame.lookVector*0.2
            Laser.CFrame = Laser.CFrame + Laser.CFrame.lookVector*0.2
        end


        wait(2)
        OnCooldown = false
        return
    end
end

I apologize if I'm not understandable enough, and any help is appreciated.

0
I solved this. Rheines 661 — 5y

Answer this question