Search Unity

Random rotation on an instantiated prefab.

Discussion in 'Scripting' started by cristo, Aug 19, 2019.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi, I'm trying to randomly call one of two possible rotations on a prefab. So that it either spins in one direction or another when it spawns. If anyone could please give me some feedback that would be great.
    Code (CSharp):
    1. public class  _Spin : MonoBehaviour {
    2.    public float Spin = 5f;
    3.  
    4.  
    5.     // Use this for initialization
    6.     void Start() {
    7.  
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.      
    14.        
    15.         int[] spin = new int[] { 1, 2 };
    16.         var rrRandom = new Random();
    17.  
    18.  
    19.         if (Random.value == 1)
    20.         {
    21.             SpinOne();      
    22.         }
    23.         else
    24.         {
    25.             SpinTwo();
    26.         }
    27.     }
    28.  
    29.     void SpinOne()
    30.     {
    31.         this.transform.Rotate(new Vector3(0, Spin, 0));
    32.         Debug.Log("One");
    33.     }
    34.  
    35.     void SpinTwo()
    36.     {
    37.         this.transform.Rotate(new Vector3(Spin, 0, 0));
    38.         Debug.Log("Two");
    39.     }
    40.  
    41. }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I believe that it would be helpful if you told us what you are seeing from your log, and why you think that is wrong (i.e. what you want to see).

    From the code I'm seeing here, I suspect that you are seeing a lot of 'TWO' in your log. This is because you are making a direct, exact comparison of Random.value (a float) with 1. This will literally be true only once in a billion, since random.value returns a number between 0.0 and 1.0, so all (well, 99.99999999999% of them) checks will drop through to 'two'

    If you want a 50/50 chance check, see if (Random.value < 0.5) for 'one' else 'two'. To avoid confusion with the numbers, perhaps rename the states from 'SpinOne' to 'SpinA' (rotate y) and rename 'SpinTwo' to 'SpinB' (rotate x) :)
     
    Last edited: Aug 19, 2019
    cristo likes this.
  3. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    Random.value is giving you a float value between 0 and 1, not 1 or 2. That's why this code will mostly go to else condition.

    To achieve what you are trying to do, you should use Random.Range. It will go something like this:
    Code (CSharp):
    1. if (Random.Range(1, 3) == 1)
    2.         {
    3.             SpinOne();  
    4.         }
    5.         else
    6.         {
    7.             SpinTwo();
    8.         }
     
    cristo likes this.
  4. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for all your feedback. I do see a lot of Two in the console. Sometimes Two One at the same time. You've both given me some insight about approaching the problem so thanks :)