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

Pointing a character's torso in the direction of the camera?

Asked by
Vexture 179
7 years ago
Edited 7 years ago

I've been working on a rudimentary 3rd person camera. At the moment, I'm trying to get the torso to point in the direction the camera is pointing. I've tried multiple methods, but I can't figure out the math behind it.

Here's my code:

rs.RenderStepped:connect(function() 
    torso.CFrame = torso.CFrame,camera.CoordinateFrame --on this line, im trying to point the torso in the direction the camera is facing.
end)

I won't bother showing the entire script, as this is the only section that doesn't work as intended. Assume all variables are defined (the ones you should be concerned about are torso, camera, and perhaps mouse (assuming we should be using mouse.Hit.p.X instead of the camera's CoordinateFrame. Your answers are much appreciated!

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

You actually only want the heading, or yaw, of the camera here. Pitching or rolling the Torso can have... awkward effects.

Also, for compatibility, torso should be HumanoidRootPart, not the actual Torso!

rs.RenderStepped:connect(function()
    local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = camera.CFrame:components()
    local heading = math.atan2(R02, R22)
    torso.CFrame = CFrame.new(torso.CFrame.p, torso.CFrame.p + CFrame.Angles(0, heading, 0).lookVector)
end)

This might be overcomplicated, but I believe it is what you are looking for.

Ad

Answer this question