Rotate NPC to look at player when hit

I want to rotate an npc when they get hit by a player, which I have done so with lookAt(), but I want it only to affect the Orientation not the actual position. Is there another way of rotating the npc to look at the player without lookAt, or body gyros, or align orientation? Thanks

2 Likes

You could probably use CFrames. In this case, you are rotating the NPC along the Y-axis, which means you should keep track of the x-position and z-position of the player. I would create a reference point that is positioned at the player’s humanoid root part position (that is at the same height as the NPC’s humanoidrootpart so no rotation occurs on an axis other than the y-axis) and use CFrame to rotate the NPC.

local playerHumanoidRootPart = -- PLAYER HUMANOID ROOT PART HERE
local targetPosition = playerHumanoidRootPart.Position
local targetX, targetZ = targetPosition.X, targetPosition.Z

local npcHumanoidRootPart = -- NPC HUMANOID ROOT PART HERE
local targetY = npcHumanoidRootPart.Position.Y
local referencePosition = Vector3.new(targetX, targetY, targetZ)

npcHumanoidRootPart .CFrame = CFrame.new(npcHumanoidRootPart.Position, referencePosition)
2 Likes
local npc = ...
local hitPlayer = ...

npc:PivotTo(CFrame.new(npc.HumanoidRootPart.Position, hitPlayer.HumanoidRootPart.Position))
1 Like