Hello I am using Navmesh to control my enemy, and I make him go to certain points one after the other The system works fine until I try to make him wait for an amount of time, which is just not working, in my head it makes total sense, but what am I doing wrong? Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyPatrolAIController : MonoBehaviour
{
[SerializeField]
Transform destination;
[SerializeField]
List<Waypoint> patrolPoints;
public NavMeshAgent navMeshAgent;
public float totalWaitTime = 3.0f;
private int amountOfPatrolPoints;
private int currentPatrolPoint;
private float timeWaited;
private bool isPatrolMoving;
private bool isGoingForward;
private bool isWaiting;
public void Start()
{
currentPatrolPoint = 0;
isPatrolMoving = false;
amountOfPatrolPoints = patrolPoints.Count;
Debug.Log(amountOfPatrolPoints);
SetDestination();
}
public void Update()
{
if (isPatrolMoving == true && navMeshAgent.remainingDistance <= 1.0f)
{
isPatrolMoving = false;
isWaiting = true;
if(isWaiting == true)
{
timeWaited += Time.deltaTime;
navMeshAgent.isStopped = true;
if(timeWaited >= totalWaitTime)
{
isWaiting = false;
navMeshAgent.isStopped = false;
}
}
if (isWaiting == false)
{
ChangePatrolPoint();
SetDestination();
}
}
if(!isPatrolMoving)
{
isWaiting = true;
}
}
private void SetDestination()
{
if(destination != null)
{
Vector3 targetVector = patrolPoints[currentPatrolPoint].transform.position;
navMeshAgent.SetDestination(targetVector);
isPatrolMoving = true;
}
}
private void ChangePatrolPoint()
{
if (currentPatrolPoint > patrolPoints.Count - 1.0f)
{
currentPatrolPoint = 0;
}
currentPatrolPoint += 1;
}
}