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!
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:
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)
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)
-- 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
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?