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.
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:
The hinge attachments:
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))