NPC following target

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want the NPCs head look at a part via the use of its Neck Motor6D

  2. What is the issue?

  3. What solutions have you tried so far?
    Tried the solutions from dev forum and youtube but doesn’t work

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local partToLookAt:BasePart = focus:IsA("Player") and focus.Character.PrimaryPart or focus:IsA("Folder") and focus:FindFirstChild("Handle") or focus:IsA("Model") and focus.PrimaryPart or focus
	
	local neckCFrame = CFrame.new(Vector3.new(0,1,0))*CFrame.fromOrientation(
		math.rad(-90),
		math.rad(-180),
		0
	)
	
	if source==HRP or source==Head then
		local unit = (HRP.CFrame.Position-partToLookAt.Position).Unit
		
		Neck.C0 = neckCFrame * CFrame.new(Vector3.zero,unit)
		--NPC:PivotTo(CFrame.lookAt(source.Position,Vector3.new(partToLookAt.Position.X,source.Position.Y,partToLookAt.Position.Z)))
	end

Screenshot 2023-10-28 152030

I think I usually did something like…

local npc = workspace:WaitForChild("NPC")
local npcNeck = npc:WaitForChild("Torso"):WaitForChild("Neck")
local npcRoot = npc:WaitForChild("HumanoidRootPart")
local targetPart = workspace:WaitForChild("Part")

-- get the directional unit vector (in this order)
local direction = (targetPart.Position - npcRoot.Position).unit

--get the angle based on the unit vectors Y and X values. (in this order)
local angle = math.atan2(direction.Y, direction.X)

--rotate around the Z axis to turn the head? (for R6 characters this works at least)
npcNeck.C0 = npcNeck.C0 * CFrame.Angles(0,0,angle)

There is probably mistakes in here but you should get the main idea after seeing the usage of direction and math.atan2()

This will make the NPC’s head move left or right but it won’t make it move up or down. (similar math can be used if you need that though)

1 Like