Search Unity

How can I rotate multiple transforms each one to get start rotating randomly ?

Discussion in 'Scripting' started by DubiDuboni, Aug 31, 2019.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    I'm using StartCoroutine and this code is working for one transform the one the script is attached to.
    In fact this script is attached to 3 objects(Cubes) but it's rotating only the one named "Options"
    But I want that when I click the mouse on "Options" it will rotate not only the current transform but more two others overall 3 transforms. And that each one will start rotating randomly.

    If I have 3 transforms and I clicked on "Options" then transform1 will start rotating at once transform2 after random seconds for example 3 and random3 will start after 1 second. Each one should start rotating at random seconds from the mouse down click.

    The reason the script is attached to each transform is that it's only part of the script the script is longer and do other stuff too. But the rotating part I want to make it like I said above. Maybe I should make another script that will control all the transforms and attach the new script to a empty gameobject.

    But not sure yet how to do the whole rotating part.

    Code (csharp):
    1.  
    2. private void OnMouseDown()
    3.     {
    4.         if (isRotating == false && transform.name == "Options")
    5.             StartCoroutine(Rotate(5));
    6.     }
    7.  
    And

    Code (csharp):
    1.  
    2.     IEnumerator Rotate(float duration)
    3.     {
    4.         Quaternion startRot = transform.rotation;
    5.         float t = 0.0f;
    6.         while (t < duration)
    7.         {
    8.             isRotating = true;
    9.             t += Time.deltaTime;
    10.  
    11.             transform.rotation = startRot * Quaternion.AngleAxis(t / duration * 360f, Vector3.up);
    12.  
    13.             yield return null;
    14.         }
    15.         transform.rotation = startRot;
    16.  
    17.         isRotating = false;
    18.     }
    19.  
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    1. Attach that script only to the "Options" game object
    2. In the script, create an array which will hold all of your cubes
    3. Then iterate through the array and start rotating the cubes from the array at random intervals. Try this out for yourself and if it doesn't work come back here.
     
    DubiDuboni likes this.
  3. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    I did 1 and 2 but how do I make the random intervals in 3 ? Either using the Update or StartCoroutine like I did before but now I'm trying first without StartCoroutine.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Rotate : MonoBehaviour
    7. {
    8.     public float xAngle, yAngle, zAngle;
    9.     public GameObject[] objectsToRotate;
    10.  
    11.     private bool isRotating = false;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if(isRotating == true)
    23.         {
    24.             for(int i = 0; i < objectsToRotate.Length; i++)
    25.             {
    26.                 objectsToRotate[i].transform.Rotate(xAngle, yAngle, zAngle);
    27.             }
    28.         }
    29.     }
    30.  
    31.     private void OnMouseDown()
    32.     {
    33.         isRotating = true;
    34.     }
    35. }
    36.  
     
  4. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    This is a general idea. Create a coroutine that rotates a single object. Then call that coroutine from another coroutine that introduces wait time between the calls. You still have to figure out how to implement 'isRotating' variable, but that shouldn't be too hard.

    P.S. - Sorry if there are any syntax errors, didn't get the time to check this.

    Code (CSharp):
    1. private void OnMouseDown()
    2. {
    3.     if(transform.name == "Options")
    4.     {
    5.         StartCoroutine(StartRotationOfObjects());
    6.     }
    7. }
    8.  
    9. private IEnumerator StartRotationOfObjects()
    10. {
    11.     for(int i = 0; i < objectToRotate.Length; i++)
    12.     {
    13.         // Random wait period before rotation starts
    14.         yield return new WaitForSeconds(Random.Range(0,3f));
    15.        
    16.         StartCoroutine(Rotate(objectToRotate[i], duration));
    17.     }
    18. }
    19.  
    20. private IEnumerator Rotate(Transform objectToRotate, float duration)
    21. {
    22.     Quaternion startRot = objectToRotate.rotation;
    23.     float t = 0.0f;
    24.     while (t < duration)
    25.     {
    26.         t += Time.deltaTime;
    27.  
    28.         objectToRotate.rotation = startRot * Quaternion.AngleAxis(t / duration * 360f, Vector3.up);
    29.  
    30.         yield return null;
    31.     }
    32.     objectToRotate.rotation = startRot;
    33. }
     
    DubiDuboni likes this.