Limiting turret rotation relative to another part

I have a turret attached to a helicopter and I’m trying to limit it’s rotation relative to the helicopter

1 Like

You could add an invisible cube facing the front at the turret, and use it as the origin of reference. Then you can restrict the angle that the turret can form with that origin. When your mouse cursor is on a spot of the space, you can use that spot and the cube to work with the desired triangles from the difference vector. Note that, from this, you can obtain the desired angles through trigonometrical rules.

Important note, if you aim to the skybox, if there isn’t an ivisible wall, it won’t detect any block, so it won’t follow the cursor there if the system you use to aim depends on objects in the space.

I’ve tried that but I couldn’t figure it out:

					local gunPivot = myModel.GunPivot
					local gunweld = root.Welds.GunWeld

					local cf = CFrame.new(myModel.GunOrigin.Position, mousePos)
					local cfLocal = cf:ToObjectSpace(myModel.GunOrigin.CFrame)	
					local x,y,z = cfLocal:ToOrientation()
					
					x = math.clamp(math.deg(x), -45, 45)
					y = math.clamp(math.deg(y), -45, 45)
					z = math.clamp(math.deg(z), -45, 45)
					
					local finalCf = CFrame.new(myModel.GunOrigin.Position) * CFrame.Angles(math.rad(x),math.rad(y),math.rad(z)):ToWorldSpace()
					
					gunweld.Enabled = false
					gunPivot.CFrame = finalCf
					gunweld.Enabled = true

Have you tried reversing the order?

Should be gunOrigin:ToObjectSpace(cf).

Yes, but it didn’t work unfortunately

That is odd as that is what I did for my turret module. Perhaps its also due to your turret setup.

Ill just link it here so you can compare.