Using CFrames to have the player face an NPC and be at a specific distance from the NPC

I wanted to make the player face the NPC from the front with 2 studs of distance between them for a kill animation.
The main problem is that it only works with one angle.

How it should look:
correctlyfacing

Another angle:
incorrectlyfacing

Here’s the code I tried:

function moveCharacter(NPC, Char)
	NPC.Humanoid.WalkSpeed = 0
	Char.Humanoid.WalkSpeed = 0
	Char.HumanoidRootPart.CFrame = CFrame.new(NPC:GetPrimaryPartCFrame().Position + Vector3.new(0, 0, -2)) * CFrame.Angles(0, math.rad(NPC.HumanoidRootPart.Orientation.Y + 180), 0)
end

Let me know if there’s a method that works

1 Like

If you work with CFrames and multiply them like:

Char.HumanoidRootPart.CFrame = NPC:GetPrimaryPartCFrame() * CFrame.new(0, 0, -2) * CFrame.Angles(0, math.pi --[[ 180 degrees ]], 0)

The * CFrame.new(0, 0, -2) moves you forward two studs (relative to the NPC’s CFrame), and then the * CFrame.Angles(0, math.pi, 0) flips you around 180 degrees.

1 Like