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 Greg 9 · Apr 12, 2011 at 05:06 PM · transformrandomrotateupdate

keep an if statement updating per frame in function Update

I'm trying to randomly change the speed of the rotation of an object in my scene. What I want is for the object to rotate at a speed of, lets say, 10 *Time.deltaTime and keep this speed for a set amount of time (again lets say 10(seconds)).

After the set amount of time has passed, I want the script to then calculate the next randomly selected speed for the rotation and to rotate the object at this speed for another randomly selected amount of time.

The script should then repeat randomly changing the speed of rotation and the time this speed lasts for the object.

This is what I have so far:

var myNumbers = 10;

function Update () {
var chance = Random.Range(0, 4);

 //transform.Rotate(randomNumbers[-20], 0, 0);

 if (chance == 1)
 {
     Debug.Log("RandomA is Go");
     amtToMove = myNumbers * Time.deltaTime;
     transform.Rotate(myNumbers*Time.deltaTime, 0, 0);
 }

 if (chance == 0)
 {
     Debug.Log("RandomB is go");
     transform.Rotate(-10*Time.deltaTime, 0, 0);
 }

 if (chance == 2)
 {
     Debug.Log("RandomC is Go");
     transform.Rotate(20*Time.deltaTime, 0, 0);
 }

 if (chance == 3)
 {
     Debug.Log("RandomD is Go");
     transform.Rotate(-20*Time.deltaTime, 0, 0);
 }    

}

I know we can't yield in a function update, but is there anyway to keep the transform.Rotate continuing for a set amount of time, without calculating the next chance variable?

I'm sure I should be using arrays, but I can't quite figure out how to use them with integers...

Thanks in advanced, any help would be grateful.

Greg

This issue has been resolved, if anyone has a similar issue or questions about the code feel free to drop me a mail!

Greg

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
3
Best Answer

Answer by Bunny83 · Apr 12, 2011 at 05:49 PM

Your script looks really strange... I don't even get what it should do.

According to your explanation i would do something like that:

var nextChange : float = 0.0; var currentSpeed : float = 10.0;

function Update() { if (Time.time > nextChange) { nextChange = Time.time + Random.Range(2.0,10.0); // wait 2 - 10 sec. currentSpeed = Random.Range(2.0,10.0); } transform.Rotate(currentSpeed * Time.deltaTime, 0, 0); }

Comment
Add comment · Show 4 · 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 Greg 9 · Apr 12, 2011 at 06:33 PM 0
Share

That actually makes a lot of sense, you have to excuse my code, I've spent the day getting the model finished and couldn't wrap my head around the logic of this. This works superbly though, I'll have to send you a link of the finished project when we're done over here, many thanks Bunny! Greg

avatar image Bunny83 · Apr 12, 2011 at 11:22 PM 0
Share

No excuse, i admire people that work on both sides (art and code). I have absolutely no art skills, but sometimes it would be very useful...

avatar image Greg 9 · Apr 13, 2011 at 04:13 PM 0
Share

Hey, couldn't find you email but hopefully this comment should trigger a message in your mail. Just a quick thanks and to show you the project we made within a 12 hour period.

The unity web link can be found here: http://www.pasteunity.com/WebPlayer.aspx?GameID=66

Enjoy :)

avatar image Bunny83 · Apr 13, 2011 at 04:32 PM 0
Share

:D nice, but not a game yet ;)

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

Rotate Method via Update ? 2 Answers

Sun(light) rotation is not working 1 Answer

How to make a cube topple?(with accuracy) 1 Answer

Will not rotate when going different speed? 1 Answer

CharacterControllers and Transform.Rotate 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