Turret-like tracking issues

I’ve been trying this for hours, I can’t seem to figure this out.

What I’m trying to achieve is to have some sort of turret-like moving head tracking system.

The moving head however just refuses to cooperate.

I’ve ended up using the script I found in a tutorial that works with a turret in any orientation but it just refuses to properly track when I try it on my lights.

In it’s position, the light is totally wrong, but the turret works just fine.

If I place them both in the same CFrame (position and orientation), they get the same angles, but the moving head seems to be pointing in other way.
image

The only difference that I found was that the turret used hinges, meanwhile I used a Motor6D.

The selected attachment is the one I use for reference in the script:
image

The hinge attachments:
image

I can easily tilt the heads at whatever angle I want, -30 degrees, for example, tilts it down 30 degrees with no issues.

local part = game.Workspace.TrackPart
local direction = fixture.Instance.Body.Base.MeshPart.Attachment.WorldCFrame:PointToObjectSpace(part.Position)

local flatDirection = direction * Vector3.new(1, 0, 1)
local yaw = math.deg(Vector3.zAxis:Angle(-flatDirection, Vector3.yAxis))

local axis = Vector3.yAxis:Cross(flatDirection)
local pitch = math.deg(flatDirection:Angle(direction, axis))

fixture:Pan(yaw)
fixture:Tilt(pitch)

The script inside the turret:

local direction = ref.CFrame:PointToObjectSpace(targetPart.Position)

local flatDirection = direction * Vector3.new(1, 0, 1)
yawHinge.TargetAngle = math.deg(Vector3.zAxis:Angle(-flatDirection, Vector3.yAxis))

local axis = Vector3.yAxis:Cross(flatDirection)
pitchHinge.TargetAngle = math.deg(flatDirection:Angle(direction, axis))
3 Likes

If you use Motor or Motor6D as well as HingeConstraints there will be a noticeable lag issue because Motors use CFraming and HingeConstraints use physics.

Why aren’t you just welding the light to the turret?

3 Likes

There are a lot of lights controlled in a main module, that would be impossible.

2 Likes

I’m confused.
You know exactly what you are talking about with all the images and scripts you’ve provided but you weren’t really specific about what items you were referring to as the turret and the light.

You said the turret was working fine but I assumed the black turret in your first pictures was the turret and you were trying to aim the light that’s on it to the red dot. Are the first 2 pictures the turret that works fine (with a light on it) and the 3rd picture is the yaw and tilt light you are working on.

When working with HingeConstraints make sure

  • all the rotating Parts are unanchored.
  • the HingeConstraint MotorMaxTorque is high enough to rotate the assembly.
  • the Limits aren’t set smaller than the amount of rotation you need (it doesn’t look like you are using those anyway).
  • there are no Parts blocking the rotation of the assembly. You can check this by temporarily making all the Parts CanCollide false.
1 Like

The moving head is what I’m trying to fix. The turret is just one I found on a model and tried its script. That seemed to work, but the light with the Motor6D doesn’t when using the same script.

1 Like