Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Make NPC's head turn towards target using Motor6D.C0 (Neck)?

Asked by 5 years ago
Edited 5 years ago

Hello! (´• w •`)/

A day ago I started working on a script that would turn an NPC's head towards the player using the Neck Motor6D. I already made one a while ago, but it was only able to look up and down, which was easier to understand and do. For this one, I want it to look left and right as well as up and always be facing the player or target. Because the NPC would also have animations which would be played on top, I could not use DesiredAngle, so I decided to go for C0.

After browsing for a while, I was able to get a solution to some things, but a few problems always seem to keep popping up. ;w;

Here's the script:

npc = script.Parent
head = npc.Head
torso = npc.Torso
neck = torso.Neck
origc0 = neck.c0 -- haven't found a use for this yet

targ = workspace.target

while wait(.05) do
    neck.C0 = CFrame.new(neck.C0.p, targ.Position) * CFrame.Angles(math.rad(-90), 0, math.rad(180))
end

This was all fine for as long as the NPC stood completely static. Not moving or rotating. If he changed his position however, the head would no longer accurately follow the target part.

After searching for a while, I found out the problem, but still was not able to entirely fix it:

while wait(.05) do
    local direction = (targ.Position - head.Position).Unit
    local toObjSp = head.CFrame:VectorToObjectSpace(direction)

    neck.C0 = CFrame.new(neck.C0.p, neck.C0.p + toObjSp) * CFrame.Angles(math.rad(-90), 0, math.rad(180))
end

Now the NPC's head follows the target (seemingly) accurately regardless of what the NPC's position and orientation is in the game. But now the problem is that it's head is twitching? It's hard to explain without showing you a video, but I'm not sure how to do that here. Basically, every time it loops the head would now face towards the target, but as soon as it does it would do that, it would quickly jump back to it's original(?) rotation.

Does anyone have any ideas how to fix this? I've been trying to figure out for a long time now and I'm really not sure what I can do? Thank you in advance !! <3

Answer this question