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

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

Asked by 5 years ago
Edited 5 years ago

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!

0
No. Just wrap a loop around the entire code so it will always go to the players head wherever it is greatneil80 2647 — 5y
0
That's what I'm doing... Humanagon 5 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

Ad

Answer this question