So I got my character to look left to right but up and down is being weird when i get to 180 degress i think, here is the issue
My code
So I got my character to look left to right but up and down is being weird when i get to 180 degress i think, here is the issue
My code
Try using CFrame.fromOrientation and not CFrame.Angles() as it is different.
Looking at it, it says YXZ
and not XYZ
It looks more like 90 degrees in your video. As @dthecoolest and @bluebxrrybot mentioned, 90 degree angles have issues with Gimbal Lock which causes 90 degree angles to flip from one axis to another.
I uh, I didn’t mention Gimbal Lock. I wish I was that smart with CFrames, though. No idea what that means. And I don’t know the difference between fromEulerAnglesXYZ
and fromEulerAnglesYXZ
. They just say it applies angles in a different order, but does that affect anything, or is it just for fixing small errors or preformance?
Look at the link I sent for Gimbal Lock. It explains why it happens.
Here’s another link that may explain why it happens using diagrams.
It’s not atan2(X, Y)
it’s atatn2(Y, X)
. The Y
appears before the X
, unlike the image shown above.
Ok, so here is your problem: using the atan2 for the rotationX. The atan2
function returns an angle in the correct quadrant based on the numbers that are given. Since the X axis rotation only needs to be in two quadrants, normal atan will work. Second is that you should be using .fromEulerAnglesYZX
. There is a difference between YZX and XYZ. It would take a while to explain, so here is something that you can read if you want to learn about it. A visual intuition for Euler angles (Rotation vs Orientation)
game:GetService("RunService").Heartbeat:Connect(function(dt)
local difference = (player1.PrimaryPart.Position - player2.PrimaryPart.Position).Unit
local rotationZ = math.atan2(difference.X, difference.Z)
local rotationX = math.atan(-difference.Y)
player1.PrimaryPart.CFrame = CFrame.new(player1.PrimaryPart.Position) * CFrame.fromEulerAnglesYXZ(rotationX, rotationZ, 0)
end)
One thing that I would recommend is that you don’t convert the angles to degrees after the atan2
operation. You do nothing with the degree version, so it is a waste of computing power to just flip it back to the original number right after.