I am trying to get a Motor6 turret to have a limit of how far down it can point. Like the way a tank barrel can only point so low toward the ground.
I reset the Mouse Hit Position to not go lower that 5 studs. I prints out with the correct Y value but makes no difference to the turret. The turret will still allow you to point it straight down.
Anyone have a clue why?
— SCRIPT
local model_Turret = script.Parent
local part_Turret = model_Turret:WaitForChild("Turret")
local part_Hinge = model_Turret:WaitForChild("Hinge")
local motor_Turret = part_Turret:WaitForChild("Motor6D")
local service_ReplicatedStorage = game:GetService("ReplicatedStorage")
local event_MousePosition = service_ReplicatedStorage:WaitForChild("RemoteEvent")
local armOffset = part_Hinge.CFrame:Inverse() * part_Turret.CFrame
local newPosition
event_MousePosition.OnServerEvent:Connect(function(player, mouseHitPosition)
-- LIMIT DOWN MOVEMENT
if mouseHitPosition.Y == 0 or mouseHitPosition.Y <= 5 then
newPosition = Vector3.new(mouseHitPosition.X, 5, mouseHitPosition.Z)
else
newPosition = mouseHitPosition
end
print("NEW", newPosition.Y, "OLD", mouseHitPosition.Y)
-- APPLY ROTATION
local armRotation = CFrame.new(part_Hinge.Position, newPosition) * CFrame.Angles(math.pi/2, 0, 0)
motor_Turret.C0 = armOffset * part_Hinge.CFrame:toObjectSpace(armRotation)
task.wait()
end)
I’m not on PC right now so I can’t check your file however why don’t you use the limits a hinge provides?
While doing that you can also switch to using TargetAngle as well so you’ll have a smooth movement. For this to look good you’ll have to calculate angle from a base point and adjust your hinge rotation to make 0 degrees the parallel one to the tank.
Ok I discovered something interesting.
If the Y coordinate of the turret is below the limit, it just won’t care about the limit.
Set it to somewhere above the turret like 10 for example and you’ll see that it works perfectly.
I tried like 3 other methods but all of them had this issue. I’m not sure about why this happens but it surely has a solution, I’m not the master of vectors as of right now to be honest.
Other than that I noticed some things like you actually don’t use a HingeConstraint, you use a Motor6D which you don’t need to use if the turret won’t stay unanchored. If I were you, I’d use a HingeConstraint and use some physical limits.