Search Unity

Instantiate With a Random Y Rotation

Discussion in 'Scripting' started by HaydarLion, Aug 1, 2015.

  1. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    How do i instantiate a 3D object with a random y rotation?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    And if i try to change the y rotation, it doesnt work. can you give an example of how?
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    If it doesn't work, then it's because something has gone wrong- post your code to give us a better chance of being able to help you.

    As for examples, you'll find plenty of examples here on the Instantiate manual page.

    General tips: typically when you're instantiating an object you're giving it a rotation relative to the current (or some other) object. That being the case, assigning it the current object's transform.rotation or some transformation of that rotation is best. To use a quaternion that's facing a specific direction in real-world coordinates, use Quaternion.LookRotation. If you want to rotate by degrees, don't use the Quaternion's "y" value (it's not what you think it is)- use Quaternion.Euler instead.
     
    Kiwasi likes this.
  5. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    Ok i got it.
    here;s the code that worked for me:
    Instantiate(object, transform.position, Quaternion.Euler(new Vector3(90, Random.Range(0, 360), 0)));
     
    Razheem, KRWatkins, Eisnel and 5 others like this.
  6. alexjhones286

    alexjhones286

    Joined:
    Mar 28, 2016
    Posts:
    5
    Obrigado'. Brasil aqui'.
     
  7. smkzalan

    smkzalan

    Joined:
    Jan 15, 2020
    Posts:
    1
    its a bit late but how can I do this loccaly?
     
  8. viarsn

    viarsn

    Joined:
    Oct 26, 2013
    Posts:
    1
    Might Work:

    GameObject spawned = Instantiate(prefabToSpawn);
    spawned.transform.localRotation = Quaternion.Euler(new Vector3(0, Random.Range(0f, 360f), 0));
     
    SvenL likes this.