Hey, I was following a tutorial where I wanted to make the humanoid head move (I understood that there is a motor6D called the “Neck” in the torso object of my humanoid). Anyhow, I want the NPC’s head move towards where the player is.
For some reason the humanoid’s head moves side to side like this:
Code:
local NPC = workspace.NPCRig
local neck = NPC.Torso.Neck
function getClosestPlayer()
local closestPlayer, closestDistance = nil, 100
for i, player in pairs(workspace:GetChildren()) do
if player:FindFirstChild("Humanoid") and player ~= NPC then
local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < closestDistance then
closestPlayer = player
closestDistance = distance
end
end
end
return closestPlayer
end
local cframe0 = neck.C0
while task.wait() do
local player = getClosestPlayer()
if player then
if NPC.PrimaryPart.CFrame:ToObjectSpace(player.PrimaryPart.Position).Z < 0 then
-- is infront
local unitOfGaze = -(NPC.PrimaryPart.CFrame.Position - player.PrimaryPart.Position).unit
neck.C0 = cframe0 * CFrame.new(Vector3.new(0, 0, 0), unitOfGaze)
end
end
end
Checked it out as well, I think maybe it’s that - you put when you define the unitOfGaze, try changing it to local unitOfGaze = (NPC.PrimaryPart.CFrame.Position - player.PrimaryPart.Position).unit
Also, maybe instead of making it face the player’s primaryPart as it does right now, you can make it face the head, unless that’s not what you want
Edit: Checked again, it doesn’t work well when you jump, I’ll check out as well what’s up with that
Alright, changed it to negative, still sadly does not work. I even changed the object it looks at to the head, same result. This is really goofy and frankly unexpected as I followed a youtube tutorial which yielded a totally different result.
I found another post that had issues moving the neck properly
I took the solution and changed it around with the tutorial you had as well, so maybe try this?
if player then
if NPC.PrimaryPart.CFrame:ToObjectSpace(player.PrimaryPart.CFrame).Z < 0 then
-- is infront
local camDirection = (CFrame.new(NPC.PrimaryPart.Position, player.PrimaryPart.Position):inverse() * NPC.PrimaryPart.CFrame).LookVector * -1
neck.C0 = CFrame.new(Vector3.new(0, neck.C0.Y, 0)) * CFrame.Angles(0, -math.asin(camDirection.X), 0) * CFrame.Angles(math.asin(camDirection.Y), 0, 0)
end
end