Search Unity

How do i instantiate a prefab with the z axis facing down

Discussion in 'Scripting' started by spiralgear, Feb 29, 2008.

  1. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    On my instantiated object i want:

    Y: to be facing away from the hit normal ( which i have working now)
    and X: To be facing down in global coordinates, so it points the way gravity it pulling



    Code (csharp):
    1. emptycopy=Instantiate(empty,hit.point,Quaternion.FromToRotation (Vector3.up,hit.normal));
    2.        
    3.         emptycopy.rotation.x=90;
    4.  
    This doesn't work because i think its rotating the X in local space and not global,How do i get the X to point downward
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    This might seem crazy, but you could try parenting it to an empty GO, with the axis how you want it, then when it instantiates, unparent it in the awake call? Or just use timed object destructor script with a quick detatch and destroy setting?

    Ugly hacks are my specialty, but they certainly arent pretty.

    :p

    AC
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Have you considered using the Transform object's LookAt function? You could build the right target transform to look at, then use hit.normal for the world-up reference and that should do what you're looking for.


    Edit: quick-and-dirty forum code example (untested!):

    Code (csharp):
    1. // instantiate a copy of 'empty'
    2. emptycopy = Instantiate(empty, hit.point, Quaternion.identity);
    3.  
    4. // initialize the target position to look at
    5. var targetposn = hit.point + gravitydirn;
    6.  
    7. // look at the target position, aligning the y-axis against the hit normal (pointing away from)
    8. emptycopy.transform.LookAt(targetposn, -hit.normal);