Making a NPC move in front of the player

Hello! I wanted to make an NPC follow and attack a Player but it keeps trying to get to the exact same Position as the HumanoidRootPart, which makes it want to stand on top of them, which disrupts gameplay.

How would I make the NPC move in front of the Player?

1 Like

It’s some additional math and using LookVector

MoveTo(TargetRootPart.Position + (TargetRootPart.CFrame.LookVector * 2))

If the player is looking away from the enemy the enemy will try to walk through them tho’ but this should be plenty.

3 Likes

It does work but the enemy just walks in front of them, and doesn’t look towards them. Do you know how I would make the enemy face the Player?

1 Like

You can use this response I found in another DevForum post:

If you want to tween the CFrame instead, you can save that CFrame in a variable and then you can use this tutorial in order to tween it:

1 Like

I’ve tried them, and I’ve got a result where the enemy just looks at you all the time which bugs the way it walks, and it also messes up my game’s combat. I want it to look at the Player only on a horizontal axis, so that it doesn’t look up or down.

1 Like
if (TargetRootPart.Position - NPCRootPart.Position).Magnitude <= 5 then
-- TWEEN LOOKING AT AI HERE
local LookAt = CFrame.new(TargetRootPart.Position, NPCRootPart.Position * Vector3.new(1, 0, 1))

It’s not a good fix but should do. I would more recommend applying changes to the Orientation property of RootPart.

3 Likes

Thanks for all your suggestions and help guys, I got it to work!

1 Like