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)
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()
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)
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.
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)
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
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)