- Home /
Won't run when clicked during animation.
The char. is patrolling waypoints. At each waypoint he will stop and an animation will play. If you click on the Char. he will run to another set of waypoints. The issue is, while the Char. is stopped at the waypoint, if you click on him, he wont run. He should run at anytime he is clicked on. Navmesh and mecanim is being used.
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 (!isClicked)
Patrolling ();
}
}
void Running ()
{
nav.speed = runSpeed;
if (canCheckDistance) {
if (Vector3.Distance (transform.position, posToCheck) > distanceLimit - 0.5f) {
isClicked = false;
canCheckDistance = false;
}
}
}
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 || waitTimeSet)
{
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;
}
}
All this code with boolean checks is very prone to logical errors. You should ins$$anonymous$$d use events. Unity 4.6 has added events implemented by default. Add an event trigger component to your game object. $$anonymous$$ake sure that there is also an object with an event system component in the scene.
Then you can add a PointerClock event where you will add the function to be called.
Answer by Mmmpies · Feb 23, 2015 at 09:03 PM
Had a look at this as I have most/all of your code from the last question!
Anyway change your Update to this:
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 ();
}
}
And change the OnMouseDown to this:
void OnMouseDown()
{
isClicked = true;
StartCoroutine(WaitForSomeTime());
if(anim.GetBool(patrolWayPoints[wayPointIndex].name)) // added by Mmmpies start
{
waitTimeSet = false;
anim.SetBool(patrolWayPoints[wayPointIndex].name, false);
patrolTimer = 0;
} // added by Mmmpies ends
Vector3 markerPos = markers[Random.Range(0, markers.Length)].transform.position;
GetComponent<NavMeshAgent>().destination = markerPos;
posToCheck = markerPos;
canCheckDistance = true;
}
Not sure what you want to happen but it appears to work for my copy of your project.
That's awesome bro! it worked right away, thanks again! Sorry for slow response I was making some supper.
$$anonymous$$mmm supper time where I am, supper can be pies right! ;-)
Glad it helped.
Your answer
Follow this Question
Related Questions
How To Play Animation On UI Button Click Please... Im Going To Freak Out Please Enter And Help Me 2 Answers
How to play sprites animation on the place that we click? 0 Answers
Navmesh Enemy AI, Syntax issue? 1 Answer
How can I stop an animator playing 0 Answers
[Mecanim] How To Play Animation? 1 Answer