Rotate NPC to face the same way as another NPC

Hello, I’ve been trying to make a script that orientates itself with anther NPC. If the main NPC is facing one way, the other NPC will be facing the same way.

Here’s an example of what I am talking about, only I want it applied to an actual player.

Here’s my code listed below:

local part = script.Parent --// Part you want to change facing
local part2 = script.Parent.Parent.Part2 --// Part direction you face towards

while wait() do
	local negativeX = part2.Orientation.X * -1
	local negativeY = (part2.Orientation.Y)
	local negativeZ = part2.Orientation.Z * -1
	part.Orientation = Vector3.new(negativeX, negativeY, negativeZ)
end

Any and all help is appreciated thank you in advance! :smile:

5 Likes

You want the “parts” to orientate in the same direction as the player’s orientation, correct? Before I provide an answer I’d like some clarification to whether or not we’re on the same page. This is the desired result:

image

Correct?

3 Likes
local npcToRotate = -- you want to rotate
local targetNPC = -- you want to face same as

local lookVector = (targetNPC.Position - npcToRotate.Position).unit
npcToRotate.CFrame = CFrame.new(npcToRotate.Position, npcToRotate.Position + lookVector)
3 Likes

Correct, I would like there body parts to face the same way.

2 Likes

I’ll give this a try now though would I include the primary part in the 2 variables above?

1 Like

I’ve tried this, it doesn’t work it only faces the player.

1 Like

Really, odd … did you fill in the locals with right NPCs

1 Like

I did yes, with the primary part included into the variables as such:

local npcToRotate = script.Parent.PrimaryPart -- you want to rotate
local targetNPC = script.Parent.Parent.Dummy2.PrimaryPart -- you want to face same as

local lookVector = (targetNPC.Position - npcToRotate.Position).unit
npcToRotate.CFrame = CFrame.new(npcToRotate.Position, npcToRotate.Position + lookVector)
1 Like

This should operate how you want it to:

-- variables
local character -- this is the reference to your player's character (or optional NPC)
local rig -- reference to the npc

-- functions
local function updateRigCFrame()
	local direction = character.PrimaryPart.Orientation.Y
	local rigPosition = rig:GetPivot().Position
	
	rig:PivotTo(CFrame.new(rigPosition) * CFrame.Angles(0, math.rad(direction), 0))
end

while true do
	updateRigCFrame()
	
	task.wait()
end
3 Likes

Wow this works flawlessly thanks so much! This is absolutely amazing, I’ve never even heard of some of these functions you’re using like :PivotTo() and math.rad are these newer functions?

1 Like

:PivotTo() is the equivalent of :SetPrimaryPartCFrame(), with the only benefit being that float point errors don’t encourage “fake” positioning.

As for math.rad(x), it’s short for “radian”. Here’s some more information about it. In short, it’s a conversion function between degrees to radians.

1 Like

Awesome thanks so much for the help and information I really appreciate it! I’ll make sure to read up on these.

1 Like

Gezz that’s touchy …

Try this, make sure PrimaryPart is set on both.

local npcToRotate = workspace:WaitForChild("Rig1").PrimaryPart
local targetNPC = workspace:WaitForChild("Rig2").PrimaryPart

targetNPC.CFrame = CFrame.new(targetNPC.Position, targetNPC.Position + npcToRotate.CFrame.LookVector)

1 Like

Awesome, thanks so much man! (char limit)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.