Following and Wondering help

Hi guys, just been messing with this AI and enjoying.

Got a bit stuck, I’ve created a simple bot that follows a player ship if it comes in to range but when it’s left that range will go back to a default area. This works fine but I’m now trying to use a snippet of code from the documentation for wondering “https://www.arongranberg.com/astar/docs/wander.html”.

I have used this code on it’s own and work great, but what I’m trying to do is when the player ship is out of range then the bot should head back to the default area and when within a certain distance to start wandering around a specified radius from the default area until the player ship comes back in to range.

I keep getting StackOverflowException. Can someone point me in the right way please.

The code is a bit hack n slash but just trying it out. It’s firstly checking is the player is in range which is highest priority so just heads that direction if the distance is less than the following range.
If it’s out of the range it should head to the default position (protectArea) if this has been defined until it gets within a distance of 100 then if a wonderRadius has been set then should kick in to wondering about.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Pathfinding
{
[UniqueComponent(tag = “ai.destination”)]
[HelpURL(“http://arongranberg.com/astar/docs/class_pathfinding_1_1_a_i_destination_setter.php”)]
public class EnemyContoller : VersionedMonoBehaviour
{

    public Transform[] targets;
    public float followRange;
    public Transform protectArea;
    public float wonderRadius;

    [HideInInspector]
    public Transform target;


    IAstarAI ai;

    Vector3 PickRandomPoint()
    {
        var point = Random.insideUnitSphere * wonderRadius;

        point.y = 0;
        point += ai.position;
        return point;
    }

    void OnEnable()
    {
        ai = GetComponent<IAstarAI>();
        if (ai != null) ai.onSearchPath += Update;
    }

    void OnDisable()
    {
        if (ai != null) ai.onSearchPath -= Update;
    }

    void Update()
    {

        float distance = 0.0f;
        int currentTarget = 0;
        float closest = (targets[0].position - transform.position).magnitude;

        for (int i = 0; i < targets.Length; i++)
        {
            distance = (targets[i].position - transform.position).magnitude;
            if (distance < closest)
            {
                closest = distance;
                currentTarget = i;
                target = targets[currentTarget];
            }
        }

        if (distance < followRange)
        {
            if (target != null && ai != null)
            {
                ai.destination = target.position;
            }
        }
        else if (protectArea != null && (protectArea.position - transform.position).magnitude > 100)
            if (!ai.pathPending && (ai.reachedEndOfPath || !ai.hasPath))
            {
                ai.destination = protectArea.position;
            }
        else if (wonderRadius > 0)
        {
            ai.destination = PickRandomPoint();
            ai.SearchPath();

        }
    }
}

}

Well. At least I can quickly help you with your StackOverflowException. You have registered to the onSearchPath callback which is called when the agent searches for a path. Inside the Update method you call ai.SearchPath… which will in turn call Update which will then again call ai.Searchpath which will call Update… And thus and infinite loop and a StackOverflowException.

Thanks Aron, for getting back to me.

I decided I didn’t really know what I was doing… so like every man I started to read the instructions after I tried to assemble!!!

OK, I followed to how to make your own AI Script and made an AI script just like in the tutorial, but what I really want is everything you have in the AI Path component (Script) but just to add in a few other checks like if a target comes within a defined range then it starts following and when out of range either stops or returns to a position defined and maybe start a roam or patrol.

I’m good with coding these checks but I’m really not sure how to add it in to the AI Path Script correctly. Do I create a new script copied form this and add my code in somewhere? I had a script called EnemyController that moved the enemy and controls shooting amongst other things running with the AI Path component. This worked great and it would following the target within a range then move back to destination if out of range. At this point added the wonder bit of script but had an stackoverflow problem!

This is where I started reading the Tutorials and seem to be back at where I started. I’m not really a hard core coder and I’m probably like a lot of people and just trying to have a go.

If you or anyone reading this post, have any pointers to best go about this. would be much appreciated.

J

Hi

Sorry for the late answer.

I would recommend that if you can you should do things from a separate script. All movement scripts are intended to be easily controllable from another script. Here is the IAstarAI interface which they all implement: https://arongranberg.com/astar/docs/iastarai.html. You would modify its destination property as you see fit, and set isStopped to true when you want it to stop temporarily.

GetComponent<AIPath>().isStopped = true;