Why does this not work

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)

– FILE

Turret.rbxl (54.3 KB)

2 Likes

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.

1 Like

Also about your code, you used 0 as the base however is your baseplate or terrain at the 0 Y coordinate? If not, it won’t work like you wanted.

1 Like

I tried using a hinge earlier but could not get it to work with Mouse Hit Position.

I need both movements. The vertical and the rotation.

I tried using two hinges but that was a disaster.

Sorry, I was trying something with that, it made no difference with or without the 0.

1 Like

I’ll get on PC and check your code out give me a min.

Can’t you use maths.clamp for this? By checking if the angle is over a certain degree and if it is clamp it to the desired maximum angle?

1 Like

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.

1 Like

Yes math.clamp can be used for this and I actually recommend the OP to change to that for a cleaner code.

1 Like

I changed to math.clamp for limiting the Y. Thanks for the tip.

1 Like

Thanks for looking into it.

It is odd that setting it to 10 works, but I do need it to go down to as low as 5.

1 Like