- Home /
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));
     }
 }
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.
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 $$
Thanks for the help. Using rotationSpeed * Time.deltaTime really made life that much easier :)
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                