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

How could I make an NPC that will follow you based on line of sight?

I wanted to make an NPC find a path to you, but only do it when you are in its line of sight, and ill use a raycast for it, and still look believable, to look like it is searching for you rather than the actual thing it's doing which is finding a path to you.

I can make the NPC wander, but how do I implement a line of sight based pathfinding system. I don't expect a code answer, but I want to know different ways I could look at it.

Two ways I thought of were:

  1. Making the script just follow you for a certain wait time, and then switching to wander mode.

  2. Somehow count the amount of turns its made. (Won't work in this particular games case since the npc is always facing you)

Which of these ways is better? Is there a different way I can approach it?

1 answer

To check if something is in line of sight, you'd take the lookVector of the npc's head, and the Unit of your raycast towards the player, Unit:Dot(lookVector) and check if it's within your desired angle (iirc with values between -1 and 1), 1 being directly in front and -1 being directly behind it.

Note that i haven't used this myself so im not 100% sure this will work.

3 comments

0

I know how to raycast, I just want to know how to make it believable.

0

I put that in there... if you want some believable in-sight radius, i'd say something like if Unit:Dot(lookVector) > 0.2 then

0

no no, I mean the pathfinding believeable

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