How to make npc look at player head (R6) with neck?

I spent a lot of time, but this so hard, there is no tutorial for r6 character. Please, if you know how to do this , send me script then, thx. (I need look only in x direction and move only head). Target - CFrame.new(npc.Character.Torso.Position,LocalPlayer.Character.Torso.Position)
npc.Character.Torso.Neck.C0 = ?

3 Likes

Get ready for some painful math and a ton of assumptions. I wrote this for a custom character but the math still applies.

local headPos = Player.Character.Head.Position
local lv= Player.Character.Torso.CFrame.LookVector
local dist = (headPos-point).Magnitude
local diff = headPos.Y-point.Y
NECK = NECK or Neck.C0 --Grab the original position once if you don't already have it

Neck.C0 = NECK * CFrame.Angles(-(math.atan(diff/dist)), -(((headPos-point).Unit):Cross(lv)).Y, 0)
6 Likes

Yeah! Thanks, but this change head rotation to z for me, how to change to x?
Code:

        local headPos = npc.Head.Position
		local lv = npc.Torso.CFrame.LookVector
		local dist = (headPos-LocalPlayer.Character.Torso.Position).Magnitude
		local diff = headPos.Y-LocalPlayer.Character.Torso.Position.Y
		NECK = npc.NeckSave.Value
		npc.Torso.Neck.C0 = NECK * CFrame.Angles(-(math.atan(diff/dist)), -(((headPos-LocalPlayer.Character.Torso.Position).Unit):Cross(lv)).Y, 0)
1 Like

Oops I left out what the “point” variable is meant to be. You have it as torso but it should be the following:

point = camera.CFrame.Position + camera.CFrame.LookVector * 10000

I don’t understand what is point for me, I tried this (I need npc look at player torso):

		local point = CFrame.new(npc.Torso.Position,LocalPlayer.Character.Torso.Position).Position
		local headPos = npc.Head.Position
		local lv = npc.Torso.CFrame.LookVector
		local dist = (headPos-point).Magnitude
		local diff = headPos.Y-point.Y
		NECK = npc.NeckSave.Value
		npc.Torso.Neck.C0 = NECK * CFrame.Angles(-(math.atan(diff/dist)), -(((headPos-point).Unit):Cross(lv)).Y, 0)
1 Like

Ahh my mistake the point I gave you was for your own character. In this case what you had before was correct, it’s just the position of the target you want the npc to look at. As for the rotation axis issue, the C0 must be set up differently that I have it. Try switching the order of the CFrame.Angles() to find the right axis, it’s a bit tricky to tell which one you need.

Ok,thanks, i will reply soon .

Do you want it to only look at the player from the player’s perspective or from the entire servers perspective?

only from local player point of view

Yeah, i did it. Big thanks to Barothoth.This is code:

        local headPos = npc.Head.Position
		local lv = npc.Torso.CFrame.LookVector
		local dist = (headPos-LocalPlayer.Character.Torso.Position).Magnitude
		local diff = headPos.Y-LocalPlayer.Character.Torso.Position.Y
		NECK = npc.NeckSave.Value

		npc.Torso.Neck.C0 = NECK * CFrame.Angles(0,0,(((headPos-LocalPlayer.Character.Torso.Position).Unit):Cross(lv)).Y)
8 Likes