Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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?

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

error

1Invalid number of arguments: 4

1 answer

Log in to vote
1
Answered by 5 years ago
Edited by royaltoe 5 years ago

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.

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

What the lookAt parameter is doing:

1function lookAt(target, eye)
2    local forwardVector = (eye - target).Unit
3    local upVector = Vector3.new(0, 1, 0)
4    -- You have to remember the right hand rule or google search to get this right
5    local rightVector = forwardVector:Cross(upVector)
6    local upVector2 = rightVector:Cross(forwardVector)
7 
8    return CFrame.fromMatrix(eye, rightVector, upVector2)
9end
0
edited. it's lookat, not lookvector. this is how lookat is made^^ royaltoe 5144 — 5y
0
..sorry? i don't believe that's a reason to edit an answer Fifkee 2017 — 5y
0
yeah sorry just wanted to clarify thing since they're different things didn't want op to be confused ur answer is good royaltoe 5144 — 5y
Ad

Answer this question