THE VISUAL: The scene setup is a roaming enemy using waypoints (patrolling). When player is near, the enemy will chase the player and also shoot when close enough. If you MOUSECLICK on the enemy during any state Patrol/Chase/Shoot, it will cause him to walk to a set of Markers for brief time, eventually returning back to Patrol as he should.
The Problem: Everything seems to “kinda” work. It’s a bit shaky. You have to click twice on the enemy to get him to run.(ONE click is desired result) First click the enemy will walk to its patrol waypoint than stop. Second Click he will then walk to the marker? I also think the speed of the run is interfering with the speed of his patrol walk?
void Awake ()
{
markers = GameObject.FindGameObjectsWithTag("marker");
// Setting up the references.
enemySight = GetComponent<DoneEnemySight>();
nav = GetComponent<NavMeshAgent>();
player = GameObject.FindGameObjectWithTag(DoneTags.player).transform;
lastPlayerSighting = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneLastPlayerSighting>();
isClicked = false;
}
void OnMouseDown()
{
isClicked = true;
StartCoroutine(WaitForSomeTime());
Vector3 markerPos = markers[Random.Range(0, markers.Length)].transform.position;
GetComponent<NavMeshAgent>().destination = markerPos;
posToCheck = markerPos;
canCheckDistance = true;
}
void Update ()
{
if (isClicked) {
Running ();
}
else if (enemySight.playerInSight)
{
if (!isClicked)
Shooting ();
}
else if (enemySight.personalLastSighting != lastPlayerSighting.resetPosition) {
if (!isClicked)
Chasing ();
}
else
{
if (!isClicked)
Patrolling ();
}
}
void Running ()
{
nav.speed = runSpeed;
if (canCheckDistance) {
if (Vector3.Distance (transform.position, posToCheck) > distanceLimit - 0.5f)
isClicked = false;
canCheckDistance = false;
}
}