Turret Spinning Out

Quick problem, I’m working on a turret using only mouse inputs and frames (no constraints, welds, or motors). Whenever the mouse goes close enough to the turret’s origin it seems like it doesn’t know what it should do with itself, causing it to spin out. Not too sure what’s causing this or how to fix it.

Code:

local hit = Mouse.Hit
local goalCFrame = CFrame.lookAt(turret.Body:GetPivot().Position, hit.Position)
local x,y,z = goalCFrame:ToOrientation()
local clampedx = math.rad(math.clamp(math.deg(x),-30,30))
local clampedy = math.rad(math.clamp(math.deg(y),-30,30))
local clampedGoalCFrame = CFrame.fromOrientation(clampedx,clampedy,z)+turret.Body:GetPivot().Position--maintain the turret position so add it
turret.Body:PivotTo(clampedGoalCFrame)

Example:

Any help is appreciated!

Good first troubleshooting step is to add prints for the different inputs into your calculation so you can see which may be problematic when the cursor is too close to the turret. A hacky fix might be to require the distance from the mouse hit to the turret be greater than a certain number before attempting to pivot

You need to make sure that the position on the turret used to calculate the look vector is not on the moving part of the turret itself.

It looks like the mouse hit is on the turret itself, which causes it to spaz out. To fix I would just not move the turret if the hit part is the turret.

Use Mouse.TargetFilter on the turret so the mouse.hit isn’t affected by the turret.

2 Likes

These seems more like its whats going on, yeah.

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