Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make npc look at a custom position (X,5,Z)?

I'm trying to make my monster look at the player's torso while following them (This is a custom monster no humanoid) but i want it to only use X and Z i want it to the ignore the Y coordinate is that even possible.

I tried using bodygryo it focuses on the player, But only on the part left surface it completely ignore's the front surface?

-- hmr is the monster's torso
-- torso is the player's
hmr.CFrame = CFrame.new(hmr.Position,torso.Position.X,torso.Position.Z)

error

 Invalid number of arguments: 4

1 answer

✓ Accepted answer

You can use CFrame's lookAt parameter to make the monster look at the human's torso position. Because you set the y position to be 5, the monster will always be looking at the height of 5.

local customPosition = Vector3.new(torso.Position.X, 5, torso.Position.Z)
hrp.CFrame = CFrame.new(hrp.CFrame, customPosition )

What the lookAt parameter is doing:

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    local upVector = Vector3.new(0, 1, 0)
    -- You have to remember the right hand rule or google search to get this right
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
 
    return CFrame.fromMatrix(eye, rightVector, upVector2)
end

3 comments

0

edited. it's lookat, not lookvector. this is how lookat is made^^

0

..sorry? i don't believe that's a reason to edit an answer

0

yeah sorry just wanted to clarify thing since they're different things didn't want op to be confused ur answer is good

This discussion is preserved as it was. New answers, comments, and votes are closed.