Choosing random targets out of a list

Hey there,

i’m actually writing my bachelorthesis about pathfinding optimisation for stacker control systems and I’m working on a demo for it now.

Your scripts are working great but I’m actually stuck. I am using your AIPath for my seeker. All I want to do is, to choose a random target of a number of targets where he goes to. After arriving the target he shall search for a next random target.

I just tried to configure the SearchPath() method.

This is my code so far:

`

/** Private Float for Random Numbers */
private float randomNumber; 


public List<GameObject> Children;
public virtual void SearchPath () {

    GameObject container = GameObject.FindGameObjectWithTag("Ziel");

	if (target == null) throw new System.InvalidOperationException("Target is null");

	lastRepath = Time.time;

    
    //Creating a List with gameobjects of tag "Ziel"
    foreach (Transform child in transform)
    {
        if (child.tag.Equals("Ziel"))
        {
            Children.Add(child.gameObject);
        }
    }

    //Getting the size of the list
    int sizeOfList = Children.Count;



    for (int i = 0; i <= 4; i++)
    {
        randomNumber = Random.Range(1, sizeOfList);
        int tempValue = (int)randomNumber;

        target = Children[i].transform;
        //This is where we should search to
        Vector3 targetPosition = target.position;

        canSearchAgain = false;

        //Alternative way of requesting the path
        //ABPath p = ABPath.Construct (GetFeetPosition(),targetPoint,null);
        //seeker.StartPath (p);

        //We should search from the current position
        seeker.StartPath(GetFeetPosition(), targetPosition);
    }
}`

Does the AIPath script work for random targets or do I have to write my own simple script?

Cheers

Hi

I am not really sure what you are trying to do in your code, you seem to constantly adding objects to the Children list without ever removing them, and you start 5 paths, not a single one.

The easiest is to have a fixed target transform, and simply move it when reaching a target.
Use the OnTargetReached function and move the target transform there. Note that will fire once per path calculation and the script by default recalculates path at regular intervals, so that method may be called multiple times for the same target.

Well I thought in the for loop that it can’t search again cause of the boolean. And after it found a path, it jumps back to the beginning of the loop, that’s what I thought.

I will give it a try with randomly choosing the target coordinations.

Alright this Method works fine for me… but now it’s pretty much in an endless loop. Is there a way to stop it after he reached like “n” targets?

Well, you can just keep an integer counter variable. Increment that every time you pick a new target and if it reaches some value N then you simply do not pick a new target.