Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 julinzp · Dec 04, 2018 at 11:28 AM · coroutinesmechanim animationrandomly

How to make a character randomly select the action per time unit?

I am trying to make my character choose whether stand or walk each time unit randomly with higher possibility for walking. I tried to make a separate coroutine for this which i called RandomWalk, however when I play this my character never stands and the play mode stops all the time

 public class WalkScript : MonoBehaviour {
 
 
         Animator anim;
         float Sprint;
         float v;
         bool hitFlag = false;
         public List<int> turnPossibilities = new List<int>();
         private Quaternion startingRotation;
         public float speed = 10f;
     
       
         IEnumerator Walk()
         
         {
             yield return new WaitForSeconds(0f);
             Walking();
             
     
         }
         IEnumerator Stand()
     
         {
             yield return new WaitForSeconds(0f);
             Standing();
             
     
         }
         IEnumerator RandomMove(float waitTime)
         {
             while (true)
             {
                 yield return new WaitForSeconds(waitTime);
                 Choose();
             }
          
         
     
         }
        
         void Start()
         {
             anim = GetComponent<Animator>();
             startingRotation = this.transform.rotation;
             
             StartCoroutine("RandomMove");
            
           
         }
     
     
     
        
         void Walking()
         {
             v = 0.2f;
             anim.SetFloat("Walk", v);
         }
         void Standing()
         {
             v = 0f;
             anim.SetFloat("Walk", v);
         }
         void Choose()
         {
             var number = Random.value;
             if (number < 0.8f)
                 StartCoroutine("Walk");
             else StartCoroutine("Stand");
         }
     }




Comment
Add comment
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

1 Reply

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

Answer by mansoor090 · Dec 04, 2018 at 11:43 AM

Your Coroutine parameter is wrong in the start function

   StartCoroutine(RandomMove(Floatvalue?));
  
 

And Start Coroutine doesnt repeat itself. try to use

     InvokeRepeating ("Choose", 1f, 1f); 

the first 1f represents starting delay and the next 1f is the delay for repeating. and do not forget to stop coroutine when next coroutine is called.

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 julinzp · Dec 04, 2018 at 11:54 AM 0
Share

Fixing the parameter helped, thanks. But why do I need to call invokeRepeating for choose if it keeps calling choose in a while loop?

avatar image mansoor090 julinzp · Dec 05, 2018 at 09:33 AM 0
Share

you can have time control on your task using invokerepeating. where as in while loop there will be numbers of calls at a time. i will recommened you to try the difference

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

95 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 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 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 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 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

Randomly Generated Terrain 2 Answers

Brain exploding ... Problem with an internal Coroutine manager 3 Answers

Coroutines stop when another game goes fullscreen 1 Answer

I don't understand coroutines and invoke 1 Answer

Use Delay time in Abstract Class 3 Answers


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