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
1
Question by Delta · Jan 09, 2011 at 02:00 PM · animationrandomtimeaction

Random actions time based

Hi, i have this script that plays random animations on my character, works well the problem is that is frame based. The question is how would you make it so it would be time based? Also do i need to move the var random : int = Random.Range(1,5) to a start func.? It keeps giving me the error that needs to be moved to a start or awake.

var count : float = 0; var switchSpeed: float = 1000; var rangeDice: float = 5; var random : int = Random.Range(1,5);

function Update() { count++; if(count >= switchSpeed) { count = 0; CreatePlane(); random = Random.Range(1,rangeDice); } }

function CreatePlane() { switch(random) { case 4: // Plays animation 4 break; case 3: // Plays animation 3 break; case 2: // Plays animation 2 break; case 1: // Plays animation 1 break; } }

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 The_r0nin · Jan 09, 2011 at 02:03 PM 0
Share

What do you mean "time-based"? You want the actions to be performed at certain times, you want there to be a certain amount of time between random actions, etc.?

avatar image Delta · Jan 09, 2011 at 02:15 PM 0
Share

Yes. Because when testing the game on different machines the actions are performed much faster or much slower dpending on the framerate. Another example testing the game on the Editor the actions are performed much slower that on a build.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Statement · Jan 09, 2011 at 02:29 PM

// Changed switchSpeed to 1, meaning one 1 second. var switchSpeed : float = 1; var rangeDice : float = 5;

// When the next animation should play. private var nextTime : float = -1;

function Update() { if(Time.time >= nextTime) { nextTime = Time.time + switchSpeed; PlayAnimation(Random.Range(1, rangeDice)); } }

// Changed name to PlayAnimation to better reflect its purpose. function PlayAnimation(animationId : int) { switch (animationId) { case 4: // Plays animation 4 break; case 3: // Plays animation 3 break; case 2: // Plays animation 2 break; case 1: // Plays animation 1 break; } }

Note that you can probably condense this code into

var switchSpeed : float = 1; var rangeDice : float = 5;

InvokeRepeating("PlayRandomAnimation", float.Epsilon, switchSpeed);

function PlayRandomAnimation() { PlayAnimation(Random.Range(1, rangeDice)); }

function PlayAnimation(animationId : int) { switch (animationId) { // ... omitted redundant code } }

and I guess you could simply use an array with legal clips to further simplify this to

var switchSpeed : float = 1; var animations : String[];

InvokeRepeating("PlayRandomAnimation", float.Epsilon, switchSpeed);

function PlayRandomAnimation() { var index = Random.Range(0, animations.Length);
animation.Play(animations[index]); }

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 Peter G · Jan 09, 2011 at 03:00 PM 1
Share

Agreed. Voted up!

avatar image Delta_ · Jan 09, 2011 at 03:10 PM 0
Share

hehe. You guys are the best. Thank you.

avatar image
2

Answer by Peter G · Jan 09, 2011 at 02:30 PM

Use a coroutine. Its better for performance than calling Update every frame.

var spawnTime : float; var rangeDice : int = 5;

function Start () { while(true) { CreatePlane(Random.Range(1, rangeDice)); yield WaitForSeconds(spawnTime); //Wait x seconds. } }

CreatePlane (value : int) { //Do your stuff here; }


or even easier, just use InvokeRepeating()

var spawnTime : float; var rangeDice : int = 5;

function Start () { InvokeRepeating("CreatePlane", spawnTime, spawnTime); //InvokeRepeating("Method", delayTime, repeatTime);

}

CreatePlane () { var randomValue : int = Random.Range(1, rangeDice); //Do your stuff here; }

Comment
Add comment · Show 5 · 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 Statement · Jan 09, 2011 at 02:40 PM 1
Share

Great $$anonymous$$ds think alike. ^^

avatar image Delta_ · Jan 09, 2011 at 03:13 PM 0
Share

Thank you for the help guys.

avatar image Statement · Jan 09, 2011 at 03:32 PM 0
Share

I am just a bit curious about how a coroutine has better performance over Update?

avatar image Peter G · Jan 09, 2011 at 04:10 PM 0
Share

It is my understanding that Update is called via Reflection internally and that isn't real great for performance. So using coroutines basically just saves method calls which saves the method call overhead + reflection just like InvokeRepeating() is more efficient than Update as well.

avatar image Peter G · Jan 09, 2011 at 04:14 PM 0
Share

If you want to see this, create a custom Update system have an event that fires once per frame using a coroutine i.e. void Start() { while(true) { eventFire(); yield return null; } } then ins$$anonymous$$d of Update, attach delegates to your event for each "tick" i.e. void Start () { someClass.eventFire += someDelegate(someUpdateFunction); } If you do some tests, you should notice that a custom tick manager is more efficient than calling Update().

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

No one has followed this question yet.

Related Questions

Timed event Question 2 Answers

Random AudioSource and Animation? 0 Answers

How can I read time from clip that is PlayQueued? 1 Answer

Randomized weather, little stuck 1 Answer

Limiting random actions (c#) (newbie) 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