Distance to Waypoint issue

Hey all,

I’ve been stuck on a piece of code that is starting to drive me crazy so I wanted to see if anyone else can spot the issue. In my FSM I have a patrol state that I am building, its fairly simple script to see if its in patrol state, get random patrol point from a list and move to it. When it gets to less then 1 distance from the way point it grabs another way point and repeats the process. The issue is with my Industrial ships, I have a bool called industrialbehavior so that when these ships reach the way point they just destroy themselves instead of repeating the process. For all other ships the script is working perfectly but for the industrial ships they just spawn and destroy themselves. As you see in the code below I added a debug log to state when they arrive and the name of the destination. when they spawn they announce they have arrived and state the proper destination even though the distance to that destination is still higher then 1. If anyone can spot what is going on with my code id appreciate it!

void FixedUpdate()
{
    //If in patrol state lets patrol through the waypoints
    if (state == ship_states.Patrol)
    { 
        //Get distance to random patrol as long as its not null
        if (randomPatrol != null)
        {
            distanceToWayPnt = (randomPatrol.transform.position - this.transform.position).magnitude;
        }
        //If Move is true then lets move to the waypoint
        if (moveToWayPnt == true)
        {
            //Move to waypoint
            this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(randomPatrol.transform.position - this.transform.position), .5f * Time.deltaTime);
            this.transform.position += this.transform.forward * .5f * Time.deltaTime;
        }
        else
        {
            //If move is not true lets get another waypoint
            PatrolRoute();
        }
              
        if (distanceToWayPnt < 1)
        {
            //industrial ships destroy themselves when they reach the waypoint
            if (industrialbehavior == true)
            {
                Debug.Log("I have arrived at: " + randomPatrol);
                //Destroy(gameObject);
            }
            //all others find the next waypoint
            else
            {
                moveToWayPnt = false;
            }
        }                         
    }
}
   
//Get the next waypoint
public void PatrolRoute()
{
    randomPatrol = patrolPoints[Random.Range(0, patrolPoints.Count)];
    moveToWayPnt = true;
}

It can happen if you didn’t run PatrolRoute() in init fuction before any FixedUpdate(). Then randomPatrol would be null so distanceToWayPnt would be 0 so your patrol would finish in first FixedUpdate. It is bugged for all objects, but other objects just pick next random point so you didn’t see bug earlier.

Other than that it’s hard to say without full script.

I had initially wondered if it was not getting the randomPatrol before doing the distance which is why I added the Debug.Log to announce the destination just to make sure, and it is calling out the correct randomPatrol point, but when it first spawns. I also tried to enclose the distanceToWayPnt check within a randomPatrol != if check but it didnt change anything. But part of the above code shouldnt even get a distanceToWayPnt if there isn’t a randomPatrol object.

Outside of the above, there isn’t much more script. This script is on the ship itself and on Awake() it has moveToWayPnt = false and state = ship_states.Patrol. The ships that currently work are already preset in the scene and have the List of patrolPoints predefined based on where they are located. The Industrial ships are spawned using another script on the SpaceStation using the below code

void Awake()
{
   InvokeRepeating("SpawnNPC", spawntime, spawntime);
}

void SpawnNPC()
{
        //Random ship and random destination
        GameObject randomship = spawnships[Random.Range(0, spawnships.Count)];
        GameObject randomDest = spawndestinations[Random.Range(0, spawndestinations.Count)];
        //Create new random ship
        var npcship = Instantiate(randomship);
        //Set ship position and send it the random destination
        npcship.transform.position = spawnPoint.transform.position;
        npcship.GetComponent<FSMController>().patrolPoints.Add(randomDest);
    }

Why not? If randomPatrol is null then distanceToWayPnt would be default value so probably 0 which is less than 1 so that’s why if FixedUpdate is before first PatrolRoute it will destroy industrial ship immediately.

1 Like

also you should not grab a patrol position from the list before checking that the list actualy has an element in it

i see you set movetowaypoint without even knowing there is a waypoint grabbed from the list .

then finally the best is to move the spawnpoint creation towards an Start event of the ship object, so it executes before the first update and not after .

edit: another point is that , you would most likely want to destroy the ship on the last waypoint , so it could be better for you to not use distance alone , but also check that the waypoint index is the last in the list before destroying .

1 Like

Thanks all for the advise all. Ill see if dropping PatrolRoute() in the start or awake function to see if that gets things sorted out in the proper order.

With these I am not worried about checking way point index because they will only have 1 way point in the list if they are Industrial ships. I have three space stations so each one spawns an industrial ship, chooses one from the two other stations and heads to it. Trying to make a “highway route” type of feeling between all three stations.