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 4 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?

-- 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

Log in to vote
1
Answered by 4 years ago
Edited by royaltoe 4 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.

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
0
edited. it's lookat, not lookvector. this is how lookat is made^^ royaltoe 5144 — 4y
0
..sorry? i don't believe that's a reason to edit an answer Fifkee 2017 — 4y
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 — 4y
Ad

Answer this question