Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ouija · Feb 23, 2015 at 07:35 PM · animationnavmeshclickwaypointrun

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;
 }
 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raiden-Freeman · Feb 23, 2015 at 08:00 PM 0
Share

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.

avatar image Ouija · Feb 23, 2015 at 08:08 PM 0
Share

Thanks man, I'll give it shot and see how it goes

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ouija · Feb 23, 2015 at 10:23 PM 0
Share

That's awesome bro! it worked right away, thanks again! Sorry for slow response I was making some supper.

avatar image Mmmpies · Feb 23, 2015 at 10:29 PM 1
Share

$$anonymous$$mmm supper time where I am, supper can be pies right! ;-)

Glad it helped.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges