CFrame.LookVector is messed up

So I am trying to make a turret that will rotate facing towards a target position at all times, and it is working to some degree. However, I am having trouble getting the horizontal rotation to work. I am using HingeConstraint allow for it to spin. For some reason, it won’t spin past a certain angle. in both direction.

Here is the video:

Here is my code:

local model = script.Parent
local hingeHorizontal = model.HorizontalRotatingPart.HingeConstraint
local hingeVertical = model.VerticalRotatingPart.HingeConstraint

local target = workspace.Enemies.Enemy.HumanoidRootPart
local sentry = model.VerticalRotatingPart

while wait() do
	local newCFrame = CFrame.new(sentry.Position, target.Position)

	hingeHorizontal.TargetAngle = math.deg(newCFrame.LookVector.Z)
	hingeVertical.TargetAngle = -math.deg(newCFrame.LookVector.Y)
end

Note: hingeVertical works completely fine. hingeHorizontal is the problem.
Also, when the angle is supposed to be 90 or -90, it get stuck around 57 or -57 degrees.
At about 50 or -50 degrees, its rate of change starts to get smaller and smaller until it reaches ± 57 degrees.

I don’t know what I am doing wrong. Can someone please help me?

The video is broken, but I think I know what’s going on.

It’s not working because that’s not what LookVector represents. It’s a vector representing where it’s facing, but not like the Orientation property.


(This is 2d, but this applies to 3d space)
Edit: Theta is in radians.

1 Like

You could also use CFrame.lookAt() unless there is a reason you need a HingeConstraint.

2 Likes

I think I forgot my pre calculus after school last year. Anyways, I somehow remembered the unit circle and I was able to solve my problem. I think it was because math.atan() is meant to return an angle in the unit circle and lookVector.Z is just a number that I have to use in an inverse trig function. I need to start studying for calculus. Thanks a lot! :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.