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

[ANSWERED] How do I make my npc move toward a player's general direction instead of the parts?

I'm having an issue where the npcs will stop moving for a moment when they get too close because in the code they reached the point where my head was, but my head isn't actually in that location anymore. I think this could be fixed by making the npc move toward the direction of the player relative to the npc's head instead of trying to move the npc to the player's head, but I don't know how to do that. Please help!

2 comments

0

No. Just wrap a loop around the entire code so it will always go to the players head wherever it is

0

That's what I'm doing...

1 answer

I thought about it and came up with a solution.

npchumanoid:MoveTo(playerhead.Position + (playerhead.Position - npchead.Position) * 2)

The way it works is, first, it subtracts the npc head position from the player head position to get the distance. (Example: (25, 0, 25) - (10, 0, 10) = (15, 0, 15) or (25, 0, 25) - (50, 0, 50) = (-25, 0, -25)) After that it multiplies the distance by 2 to make it longer. Then it adds the multiplied distance to the player head position to put the vector value past the player's head, that way defined position is a bit behind the player's head. Basically, it creates a mirror image of the npc and puts it on the other side of the player, then the npc moves to the mirrored npc.

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