I needed an AI that would through a series of checkpoints that are empty objects within the game world. Here is the code for the AI, moving through rigidbody.
public Transform[] checkpoints;
private int i;
private NavMeshAgent agent;
private Rigidbody rb;
[Header("Steering")]
public float sight;
public LayerMask layer;
private bool onYourRight, onYourLeft;
// Start is called before the first frame update
void Start()
{
i = 0;
agent = GetComponent<NavMeshAgent>();
rb = GetComponent<Rigidbody>();
agent.updatePosition = false;
agent.updateRotation = false;
agent.SetDestination(checkpoints[0].position);
}
// Update is called once per frame
void Update()
{
if (agent.pathStatus == NavMeshPathStatus.PathComplete)
{
Debug.Log("YOU REACHED IT");
i++;
Debug.Log(i);
this.agent.SetDestination(checkpoints*.position);*
}
}
private void FixedUpdate()
{
rb.velocity = agent.velocity;
agent.nextPosition = rb.position;
}
It should hit that if statement in Update() and move to the next checkpoint in the array. And yet it doesn’t. Why? What am I not seeing? What did I do wrong?
Please answer, this has been troubling me for the past hour.
----------
Update:
I diagnosed the problem in this. The agent skyrockets i to the last index so there is no where else to go. Changed update() to:
if (i < checkpoints.Length)
{
if (this.rb.position == checkpoints*.position)*
{
Debug.Log(“YOU REACHED IT”);
i++;
Debug.Log(i);
this.agent.SetDestination(checkpoints*.position);*
}
}
But now I got a new problem. It never goes to the next index.