I need help with the math for this problem

So I am making a stealth game where if you throw a rock, it causes the NPCs to look at where the rock was thrown, for example if you threw the rock at a wall, all the NPCs in the area would hear it and look at the rock.

The problem is I don’t know what the math formula would be for the rotation/cframe of the NPCs HumanoidRootPart in order for it to be looking at the Raycast’s hit position. I have an image to display what I am trying to achieve.

You can use CFrame.lookAt() in order to achieve this, for example:

local npcHRP = NPC.HumanoidRootPart
npcHRP .CFrame = CFrame.lookAt(npcHRP.Position, Vector3.new(raycast.Position.X, npcHRP.Position.Y, raycast.Position.Z))

The first position is the position of the NPC using its HumanoidRootPart.
The second position is a custom vector that is at the raycast position but also at the same height of the NPC

2 Likes

Thank you so much. I didn’t even know CFrame.lookAt() was a thing.

2 Likes