Get the Y Rotation axis from a CFrame

Hello. I’m currently making a Tower Defense game and units are capable of moving around, however, I used CFrame.Lookat for them, which causes the units to stand still when moving. I tried changing it up to the Torso’s Motor6D but it hasn’t been working for some odd reason.

function clerp(a,b,t)
       return a:lerp(b,t)
end
local lookAtFrame = cf.lookAt(torso.Position, vec(target.X, torso.Position.Y, target.Z))
rootJoint.C1 = clerp(rootJoint.C1, CFrame.new(0.026, 0, 0.47) * ang(0, rad(lookAtFrame.Rotation.Y), 0), 1)

Am I doing something wrong?

The problem is that you’re doing .Rotation on a CFrame which, if you’ve ever printed it, is not your “normal” rotation. It’s a CFrame with a position of (0, 0, 0) and a set of 3 vectors, the UpVector, RightVector, LookVector (or BackVector? I don’t remember anymore). This is used to prevent gimbal lock when doing complex CFrame calculations.

If you expect 3 rotational numbers, that’s what :ToOrientation() is for. It can only estimate the rotation due to vectors not being able to get around gimbal lock but will probably be fine in this case.


Just noticed that I said use the Y-Position of the orientation, I meant to say use the Y value/Y-Rotation.


Take note that the output of ToOrientation() is already in radians so you shouldn’t convert again.

Also, what’s the point of the clerp function if you can just do
rootJoint.C1 = rootJoint.C1:Lerp(...)

I tried. It doesn’t work, it just prints “Argument 3 missing”.

local lookAtFrame = cf.lookAt(torso.Position, vec(target.X, torso.Position.Y, target.Z))
local findY = lookAtFrame:ToOrientation()
rootJoint.C1 = CFrame.new(0.026, 0, 0.47) * cf.Angles(findY)

:ToOrientation() returns a Vector3 with (rx, ry, rz). Also, CFrame.Angles() requires 3 arguments: (rx, ry, rz), so inputting just a Vector3 doesn’t work.

local findY = lookAtFrame:ToOrientation().Y
rootJoint.C1 = CFrame.new(0.026, 0, 0.47) * cf.Angles(0, findY, 0)

image
That doesn’t work either. I’ve done it before you said that aswell, and it’s the same result.

Try using :ToEulerAnglesYXZ() idk

Whoops, I thought :ToOrientation returned a vector but apparently it returns a tuple.

Do:

local findY = table.pack(lookAtFrame:ToOrientation())[2]

Edit: You can also do this by directly putting in the tuple:

rootJoint.C1 = CFrame.new(0.026, 0, 0.47) * cf.Angles(lookAtFrame:ToOrientation())

I managed to make a fix using a BodyGyro after adjusting it’s Origin position. Thanks for the help nonetheless.