Search Unity

How can I set random speeds once inside Update ?

Discussion in 'Scripting' started by Chocolade, Jan 13, 2019.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Spin : MonoBehaviour
    7. {
    8.    [Range(1, 500)]
    9.    public float[] speeds;
    10.    public bool randomSpeed = false;
    11.  
    12.    private GameObject[] objectsToSpin;
    13.    private bool randomSpeedInUpdate = true;
    14.  
    15.    // Start is called before the first frame update
    16.    public void Init()
    17.    {
    18.        objectsToSpin = GameObject.FindGameObjectsWithTag("ClonedObj");
    19.        if (objectsToSpin.Length > 0)
    20.        {
    21.            speeds = new float[objectsToSpin.Length];
    22.            for (int i = 0; i < objectsToSpin.Length; i++)
    23.            {
    24.                if (randomSpeed == true)
    25.                {
    26.                    speeds[i] = Random.Range(1, 500);
    27.                }
    28.                else
    29.                {
    30.                    speeds[i] = 100;
    31.                }
    32.            }
    33.        }
    34.    }
    35.  
    36.    // Update is called once per frame
    37.    void Update()
    38.    {
    39.        if (objectsToSpin.Length > 0)
    40.        {
    41.            for (int i = 0; i < objectsToSpin.Length; i++)
    42.            {
    43.                objectsToSpin[i].transform.Rotate(Vector3.down, speeds[i] * Time.deltaTime);
    44.  
    45.                if (randomSpeed == true && randomSpeedInUpdate == true)
    46.                {
    47.                    speeds[i] = Random.Range(1, 500);
    48.                    randomSpeedInUpdate = false;
    49.                }
    50.                else
    51.                {
    52.                    speeds[i] = 100;
    53.                    randomSpeedInUpdate = true;
    54.                }
    55.            }
    56.        }
    57.    }
    58. }
    59.  
    If I remove randomSpeedInUpdate = true; from the else statement it will not work since randomSpeedInUpdate will never be true again.

    I want that while the game is running when changing randomSpeed between true/false it will assign random speeds or 100 to all speeds. But each time once not to assign random speeds all the time nonstop each time only once when changing randomSpeed to true.
     
  2. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    Code (CSharp):
    1.    void Update()
    2.    {
    3.        if (objectsToSpin.Length > 0)
    4.        {
    5.            for (int i = 0; i < objectsToSpin.Length; i++)
    6.            {
    7.                objectsToSpin[i].transform.Rotate(Vector3.down, speeds[i] * Time.deltaTime);
    8.                if (randomSpeed == true && randomSpeedInUpdate == true)
    9.                {
    10.                    speeds[i] = Random.Range(1, 500);
    11.                    randomSpeedInUpdate = false;
    12.                }
    13.                else
    14.                {
    15.                    speeds[i] = 100;
    16.                    //randomSpeedInUpdate = true; instead
    17.                   Invoke("BringRandomSpeedInUpdateBack",2f);// you assing the timer
    18.                }
    19.            }
    20.        }
    21.    }
    22. void BringRandomSpeedInUpdateBack(){
    23. randomSpeedInUpdate = true;
    24. }
     
    Chocolade likes this.