Object facing target using motor6Ds

Making an object face a target using motor6Ds and orientation without tweening or setting cframes.

I was trying to make a turret on a humvee rotate and face the enemy (it’s jointed to the humvee), but tweening it would make the whole humvee face the enemy.

I’ve tried tweening already, and I’m pretty sure editing the orientation of the motor6D/joint of the turret would work; however, I’m not sure how I would get the correct orientation so it’d be facing the target. I’m not very familiar with this stuff so perhaps someone can help me?

Try tweening either the C0 or C1 offset property of the Motor6D combined with CFrame.lookAt().

Tweening the offset shouldn’t affect anything else because you aren’t directly setting the object’s CFrame to anything.


The top line in this image works and makes the turret point up and down at the enemies, but the bottom line makes the turret go a bit crazy. Should I change it to be cframe.lookat?

To clarify faketorso is like the base of the turret and neck is the turret head.

I’m not sure I entirely understand your code, what does the bottom line do exactly?

I tried to make it face the target on the x and z plane and it didn’t work

I changed it to

 char.HumanoidRootPart.FakeTorso.C0 = CFrame.new(char.FakeTorso.Position.X, char.FakeTorso.Position.Y, char.FakeTorso.Position.Z) * CFrame.Angles(math.rad(50),0,0)

to test if it’d rotate the gun 50 degrees but it just made it disappear

I can’t really explain it that well, so I made this demo place to try and show you what I’m talking about:
TurretLookAtSetup.rbxl (51.2 KB)
There’s a “turret” in the middle that points towards the red cube using CFrame.lookAt().

wait i made a mistake

char.HumanoidRootPart.FakeTorso.C0 = CFrame.new(char.HumanoidRootPart.FakeTorso.C0.Position.X, char.HumanoidRootPart.FakeTorso.C0.Position.Y, char.HumanoidRootPart.FakeTorso.C0.Position.Z) * CFrame.Angles(0,math.rad(50),0)

this now makes it so the gun turns 50 degrees, but now I just need to find out how to make it so it faces the target doing this

Okay, I’ll look into that and see how it works.

I thought I had tried CFrame.lookat before and it didn’t work, I guess I’ll try again and see. Maybe I made a typo or mistake that caused it to not work.

OHH I just realized my mistake. I didn’t put C0. I’d like to ask a question though, is there any way to make that specifically x and z and not xyz? I don’t want the turret base to be able to turn up and down.

You can extract the rotations you want by using ToOrientation:

local targetCFrame = CFrame.lookAt(joint.C0.Position, workspace.Target.Position) --second value is the target's position
local _, x, z = targetCFrame:ToOrientation()
joint.C0 = CFrame.new(joint.C0.Position) * CFrame.fromEulerAnglesYXZ(0, x, z)
1 Like

Sorry if this bothers you, but I seem to have encountered an issue. The script works on non-moving objects perfectly fine, however when the humvee moves or turns the turret just acts really weirdly and I’m not sure why.

I even tested moving and rotating the turret demonstration you gave me and it starts to not point to the object accurately. Any ideas on how to fix this?

It acts as if the turret hasn’t moved and/or rotated at all making the cframe lookat very inaccurate

The CFrame is probably in object-space and not world-space. I’ll look for a solution and get back to you.

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