Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 $$anonymous$$ · Apr 16, 2015 at 05:16 AM · 2drotationquaternioncoroutinewhile

Random smooth rotation on z-axis every other second

Hi, I am a bit of a beginner when it comes to scripting but what I'm trying to do is to get a platform in a 2D space go smoothly from the current angle to a new random angle on the z-axis and then, after a delay, repeat.

Any pointers to get me heading in the right direction will be most appreciated :)

 void Start ()
 {
 player = GameObject.FindWithTag("Player");
 }
 
 void Update()
 {
 StartCoroutine(Rotation());
 }
 
 IEnumerator Rotation ()
 {    
 yield return new WaitForSeconds(startRotation);
     
     while (player != null)
     {    
     Quaternion newRotation = Quaternion.Euler (0, 0, Random.Range (minRotate, 
     maxRotate));
     transform.rotation = Quaternion.Slerp (transform.rotation, newRotation,
     rotationTime);
     
     yield return new WaitForSeconds(Random.Range(1.5f, 4f));
     }
 }


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

2 Replies

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

Answer by Ekta-Mehta-D · Apr 16, 2015 at 06:41 AM

Hii..

  float speed = 10f; 
  bool isEnabled = true; 

 IEnumerator IncreaseTheSpeed() 
 { 
      yield return new WaitForSeconds (UnityEngine.Random.Range(5f,10f)); 
      speed = Random.Range(5 , 10);
      if(isEnabled) 
      { 
              StartCoroutine (IncreaseTheSpeed ()); 
      } 
 }`


Call this once in Update. Hope this works for u..

Thanks.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Apr 16, 2015 at 08:23 AM 0
Share

$$anonymous$$any thanks. It worked like a charm :)

avatar image
1

Answer by KdRWaylander · Apr 16, 2015 at 06:43 AM

Hi,

You don't need a coroutine to do that :) Update will be enough. And instead of "rotationTime" use rotationSpeed (which would be a float) * Time.deltaTime. And that should do the trick ! Oh and prefer Lerp to Slerp, if you want the rotation to increase regurlarly you need the interpolation to be linear.

 void Update () {
     Quaternion newRotation = Quaternion.Euler(...);
     transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, rotationSpeed * Time.deltaTime);
 }

EDIT: Youps, i didn't read to the end of the sentence, didn't saw you need to repeat the rotation, coroutine will be good in this case ^^ So instead of void Update, IENumerator yourFunction () with just a little yield return new WaitForSeconds(Random.range(...)) at the end and calling the coroutine in the Update. Sorry for my mistake $$

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Apr 16, 2015 at 08:25 AM 0
Share

Thanks for the help. Using rotationSpeed * Time.deltaTime really made life that much easier :)

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

2 People are following this question.

avatar image avatar image

Related Questions

Start Coroutine after other has finished 4 Answers

void update working under conditions 1 Answer

Rotating a 2D sprite to face a target on a single axis. 3 Answers

Misunderstanding Co-routines? 1 Answer

Lock Rotation of Object between 2 points, which is looking at the direction the mouse is pointing 0 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