• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Is there a way to make universe like spore ? 4 Answers

Coroutines vs Update/FixedUpdate/LateUpdate 2 Answers

How to Invoke() .. one frame 2 Answers

Stop unnamed Coroutine instance 1 Answer

Mysteries of yield 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges