• 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 John10v10 · Nov 23, 2013 at 04:58 AM · animationrandomjava

Setting a javascript to function at random frames.

Hi, I'm trying to set up a character. It's default animation is the main "Wait". I'm trying to set it up so at a random moment or time, it will play some other animations that are meant for waiting. Like at sometime, the character turns around, or something like that. I set the parameters to when the parameter is set to 0, it plays default. When it is 1, the character will turn and look around. When it's 2, he looks back, When it is 3, he leans over. I'd like to set a javascript that will set the parameter to change from 0 from randomly 1, 2, and 3. And a frame later the parameter is immediately set back to 0. This function occurs at a random time, but not any less than every 4 seconds. Is this possible?

Thank you.

Comment
Add comment · Show 3
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 Fornoreason1000 · Nov 23, 2013 at 05:58 AM 0
Share

Yes it is possible,

first define the random range you want it to occur, you said 4 seconds the $$anonymous$$imum , i put 15 seconds as the maximum

 var $$anonymous$$ywait : float = Random.Range(4.0, 15.0);

then set up a coroutine,

 yield WaitForSeconds($$anonymous$$ywait);

then assign you "parameter" state randomly, because Random.Range will never return its max value we use 3.5 ins$$anonymous$$d of 3 then cast the return as an int

 $$anonymous$$yCharacter.state = (int)Random.Range(0, 3.5);


from here you apply the character setting from these states.

avatar image John10v10 · Nov 23, 2013 at 04:48 PM 0
Share

Thank you, but can you please type a full example javascript? I'm having trouble.

avatar image Fornoreason1000 · Nov 24, 2013 at 05:06 AM 0
Share

what part are you having trouble? the co routine? http://docs.unity3d.com/Documentation/ScriptReference/Coroutine.html are you unsure when to call the co routine? well you you want it to run when you're character's input is nothing. make a boolean value so you can stop update from recalling your coutine over an over some pseudo code as a guide

 var CouroutineRunning : Boolean = false;
 var index : int = 0;
 
 function Update() {
 
 //check if the character inst receiving input and if the co routine is already running.
 
 if(Input.Get$$anonymous$$eyDown($$anonymous$$eycode.UP)) == false && !CouroutineRunning  ) {
 $$anonymous$$onoBehaviour.StartCoroutine("RandomizeAnimIndex");
 
 }
 }
 function RandomizeAnimIndex() {
 
         CouroutineRunning = true;
         var $$anonymous$$ywait : float = Random.Range(4.0, 15.0);
     yield WaitForSeconds (5);
 
           index = (int)Random.Range(0, 4);
        CouroutineRunning = false;   
 }

also where i said 3.5 should be 4 so that they all have an equal chance(near enough) of occurring. if this is confusing you read this:

http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

it states that Random.Range will never return the max value unless max = $$anonymous$$, which i not the case.

gathering from what you said, you want to randomize phases of Idle? I would just make 1 longish animation clip. then set the playtime position.

the animation code can vary depending on your game structure.

are you using mechiam or Legacy?

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Animation Keeps Looping 2 Answers

Random AudioSource and Animation? 0 Answers

Zombie attack script animation help! 0 Answers

Random animation and InvokeRepeating 1 Answer

Random attack 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