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

Why is My lookVector Rotating Incorrectly?

Asked by 4 years ago

I'm trying to set the CFrame of the HumanoidRootPart to the same as a brick, except with the y position added by 3. I had to make a vector3 variable to do this, but now, when plugging the variable and the lookVector into a new CFrame, the lookVector isn't accurate, and rotates the HumanoidRootPart in a seemingly random (yet consistent) direction. Am I setting the CFrame wrong? What should I be doing?

game.Players.LocalPlayer.Character.HumanoidRootPart.CharacterHitBox.Touched:Connect(function(hit)
    if deb == false then
        deb = true
        local pos = Vector3.new(hit.CFrame.X, hit.CFrame.Y + 3, hit.CFrame.Z)
        game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(pos, hit.CFrame.lookVector)
        wait(0.2)
        dashdeb = false
    end
end)

1 answer

Log in to vote
2
Answered by
DanzLua 2879 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Using two Vector3s when using CFrame.new(pos,lookat) creates a cframe located at the position pos looking towards the position lookat. The reason your script isn't working is because lookVector isn't relative to the players, as it the origin of that vector is at (0,0,0) with a magnitude of 1, which represents the orientation of that part, looking forward.

To achieve what you want, we can simply take the cframe of hit and add a vector3, with y 3, to it.

game.Players.LocalPlayer.Character.HumanoidRootPart.CharacterHitBox.Touched:Connect(function(hit)
    if deb == false then
        deb = true
        game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = hit.CFrame + Vector3.new(0,3,0)
        wait(0.2)
        dashdeb = false
    end
end)
1
Thank you for the in-depth answer! SuperJumpman12 43 — 4y
Ad

Answer this question