Trying to work out how to turn tank turret

I’ve got a turret on a tank here, and I’ve been able to get it rotating, but I don’t know exactly how to work out how to rotate towards the player’s mouse. My current code for how I am rotating the turret towards the mouse is down below. All help is appreciated!

local angle = math.acos(script.Parent.Base.Pivot.Position.Unit:Dot(hit.Unit))
script.Parent.Base.PivotWeld.C1 = CFrame.Angles(0, angle, 0)
--("hit" being the mouse.Hit.Position)

Angle calculation method 1:

Math above modified for a hinge constraint, you can calculate a similar method with welds if you add attachments to your welds
(Weld.C0 and Weld.C1 is equivalent to Attachment1.CFrame and Attachment2.CFrame respectively)

Illustration with RigidConstraint as it functions the same as welds but replace the C0 and C1 with attachment CFrames.

For Motor6Ds an alternative method is using CFrame.lookAt and controlling the object space orientation with :ToOrientation()

So are you saying something like this?

local object_horizontal_offset = script.Parent.Base.Pivot.CFrame:PointToObjectSpace(hit)
local object_yaw_angle = math.atan2(-object_horizontal_offset.X, -object_horizontal_offset.Z)
		
script.Parent.Base.PivotWeld.C1 = CFrame.Angles(0, object_yaw_angle, 0)

Yes, you should try it out first.

local object_horizontal_offset = script.Parent.Base.Pivot.CFrame:PointToObjectSpace(hit)
script.Parent.Base.Pivot.CFrame --Refers to the base of the turret in which the turret is mounted on, a basepart/New Origin reference.
hit --Target position, where you want to aim at

--Atan2 refer to diagram assuming both base of turret and mounted turret is (0,0,0), also depends on the base of the turret so may need to change.
--You may also need to add an offset angle +math.rad(45) if needed
local object_yaw_angle = math.atan2(-object_horizontal_offset.X, -object_horizontal_offset.Z)

If it is spinning the base of the turret is incorrect, it should be the part that is not moving how does your rig look like?

Try using a different part as the base.

tried using a different part, the same thing happens. However, the code definitely does something because if I angle my mouse right, it flicks back and forth.

Try using the Part0 or Part1 as the base part

script.Parent.Base.PivotWeld.Part1
script.Parent.Base.PivotWeld.Part0

Like this

local object_horizontal_offset = script.Parent.Base.PivotWeld.Part1.CFrame:PointToObjectSpace(hit)


local object_yaw_angle = math.atan2(-object_horizontal_offset.X, -object_horizontal_offset.Z)

I get the exact same thing as in the previous video.

Did you try it with Part0?

local object_horizontal_offset = script.Parent.Base.PivotWeld.Part0.CFrame:PointToObjectSpace(hit)

local object_yaw_angle = math.atan2(-object_horizontal_offset.X, -object_horizontal_offset.Z)

Also how is your rig setup? I am out of ideas given the information you can dm me the model and I can give advice on what is the issue.

I believe I will need to do advanced visualization techniques to figure out the axis and how the calculation is working out in the case of your specific model.

Otherwise it should work without the spinning such as for R15 like below assuming the setup is the same:

--Insert for R15 model
local neck = script.Parent

while true do
	task.wait()
	local object_horizontal_offset = neck.Part0.CFrame:PointToObjectSpace(workspace.Part.Position)

	--Atan2 refer to diagram assuming both base of turret and mounted turret is (0,0,0), also depends on the base of the turret so may need to change.
	--You may also need to add an offset angle +math.rad(45) if needed
	local object_yaw_angle = math.atan2(object_horizontal_offset.X, -object_horizontal_offset.Z)

	neck.C1 = CFrame.Angles(0,object_yaw_angle,0)+neck.C1.Position
end

Ok I tried that, this is the result.

At least it is not spinning, which means the base is correct.

However the angle feels weird so it is probably the axis that needs changing. Try each of these lines.

	local object_yaw_angle = math.atan2(object_horizontal_offset.X, -object_horizontal_offset.Z)
	local object_yaw_angle = math.atan2(object_horizontal_offset.X, -object_horizontal_offset.Y)
	local object_yaw_angle = math.atan2(object_horizontal_offset.Y, -object_horizontal_offset.Z)
1 Like

The 2nd one worked! Thanks for your help.

1 Like

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