Turret Tracking Issue

So I’ve been trying to get turret tracking down just right, but I can’t figure out for the life of me on how to limit its angles, anyone got any suggestions on what I could do?

local absoluteTurretCFrame = CFrame.new(script.Parent.Body.TurretPos.Position, Players[i].Character.HumanoidRootPart.Position)


To be honest, I wanted to make it out of contraints so there is no issues with it falling behind (The object is moving) but it comes down to the same problem as well. (I really suck at CFRAMES and angled stuff)

(If anyone has a good weld idea that can be limited on its angles, I’d gladly switch over to that)

I’ve been able to figure out how to limit up/down movement but its mainly left and right that I have issues with the most, and everything else I tried would result in the turret not turning at all, or it would have its gun flip backwards and kinda works, but I want it to be facing forwards from the middle point, with said limits.


(This is a example of how it works currently)


You could use if axis > where you want, stop the movement part of ur script so for instance;

if part.Position.X > 2 then
turretCanMove = false

1 Like

I have that down, my issue is getting the front axis so I can limit the turning, so it can only turn 80* from the frontface (as a example).




Below is actually why I kinda want to switch to constraints but not sure how to do that one with targeting. (it falls behind when moving)


1 Like

What I would do to make it easier for myself to develop is to break up the CFrame rotation into its component x,y,z rotations. I would then manipulate that in any way I wanted to and then recreate the unit CFrame rotation with those angles.

1 Like

Use CFrame.LookVector

It will return the forward facing direction of a part.
You can then use that to find what direction the part is facing and limit how far it can turn.

In addition you can use RightVector or LeftVector to find the right and left facing directions as well.

1 Like

OP already has the rotation data, as he creates a CFrame with lookVector (TurretPos - HumanoidRootPartPos). However, what OP is asking is how to limit the angle of this vector. To do so, he needs to extract the individual angle components, limit them, then reconstruct a rotation CFrame.

Use radX, radY, radZ = CFrame:ToOrientation() to find radian values from a CFrame and then compare them to the maximum and minimum rotation values you would like.

Oh that one, that was one of the ones I tried, but it kept giving me really small numbers and wouldn’t work correctly, but I couldve just used it wrong.

Keep in mind that this rotation is with respect to world axis. You need to calculate the relative vector of the turret with respect to the tank front vector and then manipulate that. If not, then your constraints will be independent with the direction the tank is facing.

1 Like


These are the values I got earlier. (printed each one in a row), I’ve noticed that they aren’t big enough to properly adjust the turrets direction, and I couldn’t figure out the means to properly convert them to that. (I’m dumb ik) I managed to jury rig it to work before, but then it came down to the direction math and I couldn’t figure out how to properly get the angles so I could limit them, this lead to the turrets aiming in one direction, but with limits of -25 and 25 in place using math.clamp, in that one direction. (not facing forward)

Remember that you’re receiving a radian value, to simplify it you can convert radian to degrees by using the formula: Radian*180/pi

Math.clamp is a good solution to limit it’s position. To make sure it’s position doesn’t lag behind, you can run the equation not only when the mouse moves, but also when the player moves.

1 Like

This isn’t using a mouse to predict movement, its ai is basically programmed to aim at the player and other objects automatically, so using the mouse won’t really cut it for that, as for the other thing, I’ll plug that in and see how that goes thank you.

	local radX, radY, radZ = absoluteTurretCFrame:toOrientation()
	local TrueRadion = Vector3.new(radX,radY,radZ)*180/math.pi
	local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
	local radHX, radHY, radHZ = HumanoidRootPart.CFrame:toOrientation()
	local ClampY = math.clamp(radHY,TrueRadion.Y - 50,TrueRadion.Y + 50)

script.Parent.Head:SetPrimaryPartCFrame(CFrame.new(absoluteTurretCFrame.p) * CFrame.fromOrientation(radX,math.rad(ClampY),0))

IT KINDA WORKS? BUT NOT REALLY? I’m a bit lost, but if I don’t clamp it works and aims, elsewise it kinda glitchilly goes to the side. (HumanoidRootPart facing forward it seems it splits from 180 to -180 in the middle. but math.clamp cannot work with the current numbers, its prob something I’m doing.

As @jody7777 said, you need to manipulate the relative vector. In other words, after you calculate the turret CFrame to aim at the character as you originally posted, convert it to the object space of the tank. Then adjust the CFrame based on your desired maximum and minimum angles, and then convert back to world space.

Of course, there is always more than one solution to any problem, so I am not saying others solutions are wrong. Let me know if my solution does not make sense!

Yeah, I kinda get what you mean, but I don’t think I understand enough to put that into practice effectively, it could just be my brain being tired too. I’ve been spending alot on other things trying to push out something before a self imposed deadline :unamused:

Self imposed deadlines can be troublesome haha, I don’t know about you but I usually don’t give myself enough time because I am bad at guessing how long something will take… :joy:

I have to go to bed, but I will explain this in the morning if someone doesn’t beat me to it! :+1:t3:

Alright, thanks I’m prob gonna go to bed soon too, I just need to work on other parts of stuff before I do. :partying_face:

1 Like

I found this identical post that explains two different solutions you can use. I was going to give you a detailed explanation, but they have already done it. If you have any questions, just ask!

You will have to do some basic tweaking of the code (obviously you aren’t aiming at the mouse, or using 90 degrees as your maximum angle). Also note, his first post of the code has a mistake that he fixes later on in a separate reply. You also do not have a “core”, so you won’t need that (you will have to adjust a few lines for that as well).


Alright thank you I’ll take a look at it when I get more time! :face_with_hand_over_mouth:

Worked like a charm, I also fixed the turret falling behind issue while doing this, now it doesn’t fall behind at all even when moving, is a bit problematic though because it sometimes snaps behind itself if you go right on the edges, but I can look into this, prob need to put a clamp or two.

1 Like