Search Unity

Starting a game object with a random direction.

Discussion in 'Scripting' started by dead_byte_dawn, Jun 27, 2017.

  1. dead_byte_dawn

    dead_byte_dawn

    Joined:
    May 7, 2015
    Posts:
    16
    What I wanted was to have an object Start (eventually instantiated or pooled) with a random Y direction. I have this working properly in my scene but I feel like I strong-armed this solution multiplying the Deg2Rad by 10k.
    Is that going to bite me in the ass later on?

     
  2. dead_byte_dawn

    dead_byte_dawn

    Joined:
    May 7, 2015
    Posts:
    16
    Well... I just realized my own mistake. eulerAngles does all the dirty work.

    Code (CSharp):
    1.     public float deg;
    2.     public float speed;
    3.     public float startRot;
    4.  
    5.     private float rad;
    6.     private float yRotation;
    7.  
    8.     void Awake(){
    9.         RotRandom ();
    10.     }
    11.  
    12.     void RotRandom(){
    13.         startRot = Random.Range (0, 360);
    14.     }
    15.  
    16.     void TurningRad() {
    17. //        yRotation = startRot * Mathf.Deg2Rad * 10000;
    18.         yRotation = startRot;
    19.  
    20.         transform.eulerAngles = new Vector3 (0, yRotation, 0);
    21.     }
    22.  
    23.     void Update(){
    24.         TurningRad ();
    25.         transform.Translate (Vector3.forward * speed * Time.deltaTime);
    26.     }
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    dead_byte_dawn likes this.