Search Unity

How to adjust my prefab to face the correct direction when instantiated?

Discussion in 'Prefabs' started by miguelrmonreal, Nov 19, 2019.

  1. miguelrmonreal

    miguelrmonreal

    Joined:
    Nov 6, 2017
    Posts:
    2
    i've adjusted the rotation of the prefab itself and the spawn location but can't get it to face the correct direction. i've looked on online and youtube but can't seem to find the answer that works.

    Instantiate(barrel, barrel.position, barrel.rotation);
    and
    Instantiate(barrel, barrel.position, transform.rotation);
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    643
    Hi and welcome @miguelrmonreal,

    Sure you didn't find anything? I cut'n'pasted your question to google and got a few threads. You need to do some reading on them to get the basics :)

    But, here's one example. You just provide a rotation for your instantiate command. Instead of giving the barrel's rotation or the transforms rotation (which is the rotation of the GameObject your script is assigned to, you could give a rotation you set yourself (or get from code.)

    One way to do this if you want to work with Euler coordinates is to do something like this.

    Code (CSharp):
    1. Vector3 xyz = new Vector3(0, 90, 0);
    2. Quaternion newRotation = Quaternion.Euler(xyz);
    3. GameObject.Instantiate(instance, this.transform.position, newRotation);
    So now the object "instance" would face to the direction of newRotation, rotated around it's Y-axis.

    You could also use Quaternion's built-in LookRotation to easily generate a rotation direction:
    Code (CSharp):
    1. newRotation = Quaternion.LookRotation(Vector3.left);
    This would make the rotation point towards the object's left direction vector (that is the object to which your script is attached to).

    Tip: Always figure out where exactly your object is pointing to. Keep eye on those arrow colors etc. Then start trying to get your object turn to the direction you want. Especially important when you start working with more complex things like character rigs, machines with joints and so on.
     
    Last edited: Nov 19, 2019
    miguelrmonreal likes this.
  3. miguelrmonreal

    miguelrmonreal

    Joined:
    Nov 6, 2017
    Posts:
    2
    i had tried several things i had found but nothing was working. i tried to keep my search description short, next time i'll enter the whole thing into google instead. thanks for the help, i'll try these out. it was from a object i built in blender and brought into unity so i'm trying to adjust directions on it. i tried making it a child of a blank gameObject but it still keep rotating when i instantiate it. Thank you for your help.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    643
    If you parented your object as a child to an empty object, it should have worked fine. I think it's worth checking the export settings etc. and reset all transforms in Blender so that there's no unnecessary rotations/scaling etc.
     
unityunity