Attack Player From Path Distance Info

Hi guys,

I’m using the free version of A* Pathfinding Project and so far it’s work brilliantly. My monster walks around a maze with the help of way points with ease, I cannot fault the script. :smiley:

I would like to have the script to also target the player. My idea was to have the monster know the path to the player and distance. If the distance of the path is at a certain distance a piece of music will start to inform the player the monster is near by. When in view of the monster it will then chase and attack the player, etc…

Can this be done and how?



Now, the easy version is to just check the raw distance, not taking into account that the enemy might need to walk around walls which would take a long time.
This is done simply by
public void Update () { bool any = false; for (int i=0;i<enemies.Length;i++) { if ((transform.position-enemies[i].transform.position).sqrMagnitude < range*range) { // Play scary music any = true; break; } if (!any) // stop scary music }

Making it aware of the real distances it needs to walk is not that hard either.
The easiest is if you have your enemies set to recalculate the path to the player relatively often, then we can check just when the enemy repaths.
public void Start () {
GetComponent.pathCallback += OnPathComplete;

public void OnPathComplete (Path p) {
if (p.GetTotalLength() < range) {
// play scary music

Then you have another script which checks that if no requests to start the scary music have been made, say within the last 3 seconds, stop the scary music.

Thank you for your very quick replay Aron. I’ll have a try in a bit.