Today’s the day I start learning how to work with animation. I am attempting to play a different animation once the Char. reaches a wayPoint. Each wayPoint will have its own animation. I am wondering if this should be done in the script? or with the animator somehow? Anyone with ideas or examples would be awesome.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DoneEnemyAI : MonoBehaviour
{
public float patrolSpeed = 2f; // The nav mesh agent's speed when patrolling.
public float chaseSpeed = 5f; // The nav mesh agent's speed when chasing.
public float runSpeed = 10f;
public float chaseWaitTime = 5f; // The amount of time to wait when the last sighting is reached.
public float patrolWaitTime = 1f; // The amount of time to wait when the patrol way point is reached.
public Transform[] patrolWayPoints; // An array of transforms for the patrol route.
public float waitTime;
public float distanceLimit;
private DoneHashIDs hash;
private DoneEnemySight enemySight; // Reference to the EnemySight script.
private NavMeshAgent nav; // Reference to the nav mesh agent.
private Transform player; // Reference to the player's transform.
private DoneLastPlayerSighting lastPlayerSighting; // Reference to the last global sighting of the player.
private float chaseTimer; // A timer for the chaseWaitTime.
private float patrolTimer; // A timer for the patrolWaitTime.
private int wayPointIndex; // A counter for the way point array.
private bool isClicked;
private bool canCheckDistance;
private Vector3 posToCheck;
private Animator anim;
GameObject[] markers;
GameObject[] way1;
private bool waitTimeSet = false;
private int lastPoint = -1; // put at the top
void Awake ()
{
anim = GetComponent<Animator>();
hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneHashIDs>();
markers = GameObject.FindGameObjectsWithTag("marker");
// Setting up the references.
enemySight = GetComponent<DoneEnemySight>();
nav = GetComponent<NavMeshAgent>();
player = GameObject.FindGameObjectWithTag(DoneTags.player).transform;
way1 = GameObject.FindGameObjectsWithTag("way1");
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)
// Running ();
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;
}
}
}
void Shooting ()
{
// Stop the enemy where it is.
nav.Stop();
}
void Chasing ()
{
// Create a vector from the enemy to the last sighting of the player.
Vector3 sightingDeltaPos = enemySight.personalLastSighting - transform.position;
// If the the last personal sighting of the player is not close...
if(sightingDeltaPos.sqrMagnitude > 4f)
// ... set the destination for the NavMeshAgent to the last personal sighting of the player.
nav.destination = enemySight.personalLastSighting;
// Set the appropriate speed for the NavMeshAgent.
nav.speed = chaseSpeed;
// If near the last personal sighting...
if(nav.remainingDistance < nav.stoppingDistance)
{
// ... increment the timer.
chaseTimer += Time.deltaTime;
// If the timer exceeds the wait time...
if(chaseTimer >= chaseWaitTime)
{
chaseWaitTime = Random.Range(4,31);
// ... reset last global sighting, the last personal sighting and the timer.
lastPlayerSighting.position = lastPlayerSighting.resetPosition;
enemySight.personalLastSighting = lastPlayerSighting.resetPosition;
chaseTimer = 0f;
}
}
else
// If not near the last sighting personal sighting of the player, reset the timer.
chaseTimer = 0f;
}
void Patrolling ()
{
// Set an appropriate speed for the NavMeshAgent.
nav.speed = patrolSpeed;
// If near the next waypoint or there is no destination...
if(nav.remainingDistance < nav.stoppingDistance)
{
if(!waitTimeSet)
{
patrolWaitTime = Random.Range(2,4);
anim.SetBool(patrolWayPoints[wayPointIndex].name, true);
//Debug.Log (patrolWayPoints[wayPointIndex].name);
patrolTimer = 0;
waitTimeSet = true;
}
// ... increment the timer.
patrolTimer += Time.deltaTime;
//Debug.Log (patrolTimer);
// If the timer exceeds the wait time...
if(patrolTimer >= patrolWaitTime)
{
anim.SetBool(patrolWayPoints[wayPointIndex].name, false);
if(wayPointIndex != null)
lastPoint = wayPointIndex;
wayPointIndex = Random.Range(0, patrolWayPoints.Length);
//Debug.Log ("before last = " + lastPoint + " new = " + wayPointIndex);
if(lastPoint == wayPointIndex)
{
int[] tempIntArray = new int[patrolWayPoints.Length - 1];
int cnt = 0;
for (int i = 0; i < patrolWayPoints.Length; i++)
{
if(i != lastPoint)
{
tempIntArray[cnt] = i;
cnt ++;
}
wayPointIndex = tempIntArray[Random.Range (0,tempIntArray.Length)];
}
//Debug.Log ("after last = " + lastPoint + " new = " + wayPointIndex);
}
nav.destination = patrolWayPoints[wayPointIndex].position;
waitTimeSet = false;
}
}
else
{
patrolTimer = 0;
}
}
IEnumerator WaitForSomeTime()
{
waitTime = Random.Range(4,31);
yield return new WaitForSeconds(waitTime);
isClicked = false;
}
}