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?
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 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().
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)
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