Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can I rotate an object with random speed with some intervals ?

Discussion in 'Scripting' started by Chocolade, May 3, 2020.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Turn_Move : MonoBehaviour {
    7.    public int TurnX;
    8.    public int TurnY;
    9.    public int TurnZ;
    10.  
    11.    public int MoveX;
    12.    public int MoveY;
    13.    public int MoveZ;
    14.  
    15.    public bool World;
    16.  
    17.    // Use this for initialization
    18.    void Start () {
    19.  
    20.    }
    21.  
    22.    // Update is called once per frame
    23.    void Update () {
    24.        if (World == true) {
    25.            transform.Rotate(TurnX * Time.deltaTime,TurnY * Time.deltaTime,TurnZ * Time.deltaTime, Space.World);
    26.            transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.World);
    27.        }else{
    28.            transform.Rotate(TurnX * Time.deltaTime,Random.Range(3,300) * Time.deltaTime,TurnZ * Time.deltaTime, Space.Self);
    29.            transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.Self);
    30.        }
    31.    }
    32. }
    33.  
    This is the random part :

    Code (csharp):
    1.  
    2. Random.Range(3,300)
    3.  
    but sine it's changing the random numbers too quick the changes are almost not visible. I want somehow to make that if for example the next random will stay for example 5 seconds at this speed number then move to the next random number and again stay at this number 5 seconds and so on.

    Tried this :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Turn_Move : MonoBehaviour {
    7.    public int TurnX;
    8.    public int TurnY;
    9.    public int TurnZ;
    10.  
    11.    public int MoveX;
    12.    public int MoveY;
    13.    public int MoveZ;
    14.  
    15.    public bool World;
    16.  
    17.    private bool IsGameRunning = false;
    18.  
    19.    // Use this for initialization
    20.    void Start ()
    21.    {
    22.        IsGameRunning = true;
    23.        StartCoroutine(SpeedWaitForSeconds());
    24.    }
    25.  
    26.    // Update is called once per frame
    27.    void Update () {
    28.  
    29.    }
    30.  
    31.    IEnumerator SpeedWaitForSeconds()
    32.    {
    33.        var delay = new WaitForSeconds(3);//define ONCE to avoid memory leak
    34.        while (IsGameRunning)
    35.        {
    36.            if (World == true)
    37.            {
    38.                transform.Rotate(TurnX * Time.deltaTime, TurnY * Time.deltaTime, TurnZ * Time.deltaTime, Space.World);
    39.                transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.World);
    40.            }
    41.            else
    42.            {
    43.                transform.Rotate(TurnX * Time.deltaTime, Random.Range(3, 300) * Time.deltaTime, TurnZ * Time.deltaTime, Space.Self);
    44.                transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.Self);
    45.            }
    46.  
    47.            yield return delay;//wait
    48.        }
    49.    }
    50. }
    51.  
    but that just rotate the object once each 3 seconds. and not rotating it all the time with speed changing every 3 seconds.

    but now it's not rotating for 3 seconds it rotating every 3 seconds.

    better to describe what I want is like intervals. The object should spin all the time nonstop like i Update but then each 3 seconds to change for a random speed.

    for example spin at speed 5 ....after 3 seconds spin at speed 77 ....after 3 second spin at speed 199
     
  2. ItsBoats

    ItsBoats

    Joined:
    May 6, 2013
    Posts:
    7
    Wrote in notepad really quick so it's untested:

    Code (CSharp):
    1. using UnityEngine;
    2. public class Turn_Move : MonoBehaviour {
    3.    public int TurnX;
    4.    public int TurnY;
    5.    public int TurnZ;
    6.    public int MoveX;
    7.    public int MoveY;
    8.    public int MoveZ;
    9.    public bool World;
    10.    private float rotationTime = 0; //Used to track how long its been spinning for since the last reset
    11.    private float rotationDuration = 3f; //How long should it rotate for before changing rotation speed again?
    12.    private int rotationSpeed = 0;
    13.  
    14.    // Use this for initialization
    15.    void Start () {
    16.       rotationSpeed = Random.Range(3, 300);
    17.    }
    18.    // Update is called once per frame
    19.    void Update () {
    20.  
    21.  
    22.        if (World == true) {
    23.            transform.Rotate(TurnX * Time.deltaTime,TurnY * Time.deltaTime,TurnZ * Time.deltaTime, Space.World);
    24.            transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.World);
    25.        }else{
    26.            transform.Rotate(TurnX * Time.deltaTime, rotationSpeed * Time.deltaTime,TurnZ * Time.deltaTime, Space.Self);
    27.            transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.Self);
    28.        }
    29.  
    30.        //Reset rotation speed every 3 seconds
    31.        rotationTime += time.deltaTime;
    32.        if (rotationTime > rotationDuration){
    33.            rotationSpeed = Random.Range(3, 300);
    34.            rotationTime = 0;
    35.        }
    36.    }
    37. }
    Edit: forgot to reset rotationTime at end
     
    Last edited: May 3, 2020
    Chocolade likes this.