Agents Insist on moving to a universally predetermined waypoint before heading to intended waypoint

The problem arose when I created a separate script to handle mouse to handle Mouse input. The idea was to limit the expense of ray casting by only calling a raycast when a agent was selected.

The selection process works however before an agent moves to its desired location it insists go to a specific area of the graph.
The (Lets call it first waypoint) location varies with the size of the graph.

This is the AI script attahced to each agent (Some function have been commented out)
//-------------------------------------------------------------------------------------------------------------||
using UnityEngine;
using System.Collections;

using Pathfinding;
public class AstarAI : MonoBehaviour
{
public Vector3 targetPosition;
public Seeker seeker;
private CharacterController controller;

//The calculated path
public Path path;

//The AI's speed per second
public float speed = 100;

//The max distance from the AI to a waypoint for it to continue to the next waypoint
public float nextWaypointDistance = 3;

//The waypoint we are currently moving towards
public int currentWaypoint = 0;


public bool isSelected; 

public void Start()
{
    isSelected = false; 

    seeker = GetComponent<Seeker>();
    controller = GetComponent<CharacterController>();

    //Start a new path to the targetPosition, return the result to the OnPathComplete function
    //seeker.StartPath(transform.position, targetPosition, OnPathComplete);
    transform.LookAt(targetPosition);

   
}
public void OnPathComplete(Path p)
{
    Debug.Log("Yay, we got a path back. Did it have an error? " + p.error);
    if (!p.error)
    {
        path = p;
        //Reset the waypoint counter
        currentWaypoint = 0;
    }
}

public void FixedUpdate()
{
    if (path == null)
    {
        //We have no path to move after yet
        return;
    }
    //OnMouseClick();

    if (currentWaypoint >= path.vectorPath.Count)
    {
        Debug.Log("End Of Path Reached");
        return;
    }

    //Direction to the next waypoint
    Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
    dir *= speed * Time.fixedDeltaTime;
    controller.SimpleMove(dir);

    //Check if we are close enough to the next waypoint
    //If we are, proceed to follow the next waypoint
    if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance)
    {
        currentWaypoint++;
        return;
    }
}


public void OnMouseClick()
{

} 

/*
if (isSelected)
{
    if (Input.GetMouseButtonDown(1))
    {

        Debug.Log("ITs inm here");
        //create raycast source and target
        Ray tRY_Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit tRH_Hit;
        if (Physics.Raycast(tRY_Ray, out tRH_Hit, 50F))
        {

            SetTargetPos(new Vector3(Mathf.Round(tRH_Hit.point.x), GetTargetPos().y, Mathf.Round(tRH_Hit.point.z)));
            seeker.StartPath(transform.position, targetPosition, OnPathComplete);
            transform.LookAt(targetPosition);

        }
    }
}

if (Input.GetMouseButtonDown(0))
{
    Ray selection_Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit _Hit;
    if (Physics.Raycast(selection_Ray, out _Hit, 50F))
    {
        if (_Hit.collider.tag == "crew")
        {
            //  _Hit.collider.
            isSelected = true;
        }
        else
        {
            isSelected = false;
        }
    }
}*/
    // if (Physics.Raycast(selection_Ray, out _Hit, 50F))
    //  {
    //    SetTargetPos(new Vector3(Mathf.Round(Hit.point.x), GetTargetPos().y, Mathf.Round(_Hit.point.z)));
    //     seeker.StartPath(transform.position, targetPosition, OnPathComplete);
    //     transform.LookAt(targetPosition);
    //     animCompRef.GetComponent<Animation>().Play("run");
    //  }
public void SetTargetPos(Vector3 vV3_Pos)
{
    targetPosition = vV3_Pos;
}

public Vector3 GetTargetPos()
{
    return targetPosition;
}

public Seeker Seeker
{
    get
    {
        return this.seeker;
    }
    set
    {
        this.seeker = value;
    }
}

}

//-------------------------------------------------------------------------------------------------------------||
This is the Mouse input Script
//-------------------------------------------------------------------------------------------------------------||
using UnityEngine;
using System.Collections;
using Pathfinding;

public class JF_Mouse_Input : MonoBehaviour {

// Use this for initialization

public GameObject currentPlayer;
public AstarAI currentAStarRef; 

void Start ()
{

}

// Update is called once per frame
public void FixedUpdate()
{
    OnMouseClick();
}

void OnMouseClick()
{
    if (currentAStarRef.isSelected)
    {
        if (Input.GetMouseButtonDown(1))
        {  //create raycast source and target
            Ray tRY_Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit tRH_Hit;
            if (Physics.Raycast(tRY_Ray, out tRH_Hit, 50F))
            {
                currentAStarRef.SetTargetPos(new Vector3(Mathf.Round(tRH_Hit.point.x), currentAStarRef.GetTargetPos().y, Mathf.Round(tRH_Hit.point.z)));
                currentAStarRef.seeker.StartPath(transform.position, currentAStarRef.targetPosition, currentAStarRef.OnPathComplete);
                transform.LookAt(currentAStarRef.targetPosition);
            }
        }
        
    }
    //currentAStarRef.OnMouseClick();
    if (Input.GetMouseButtonDown(0))
        {
            Debug.Log(currentPlayer.name + "target position " + currentAStarRef.targetPosition);
            Ray selection_Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit _Hit;
            if (Physics.Raycast(selection_Ray, out _Hit, 50F))
            {
                if (_Hit.collider.tag == "crew")
                {
                    currentPlayer = _Hit.transform.gameObject;
                    currentAStarRef = currentPlayer.GetComponent<AstarAI>();
                    //currentAStarRef.targetPosition = currentPlayer.transform.position;
                    currentAStarRef.isSelected = true;
                }
                else
                {
                    currentPlayer = _Hit.transform.gameObject;
                    currentAStarRef = currentPlayer.GetComponent<AstarAI>();
                    currentAStarRef.isSelected = false;
                }
            }
        }
    }

}
//-------------------------------------------------------------------------------------------------------------||

If Anyone has any suggestion to this problem that would be fantastic. Or at least why it doesn’t work or why it can’t work.

If there are any other approaches people would try to implement when managing selection of multiple agents.

Feel free to ask any questions if you feel I have no provided enough information. It wouldn’t let me post more than 2 images due to being a new member so I pasted the code in text.

All the best James.

Hi

Hm, hard to say.
You can try debugging the paths you get back in the OnPathComplete callback. Somewhere you are sending a path request to the wrong position. Check the p.originalEndPoint field.