Search Unity

Question about Instantiate rotation

Discussion in 'Scripting' started by TioSlash, Nov 10, 2010.

  1. TioSlash

    TioSlash

    Joined:
    Nov 10, 2010
    Posts:
    8
    I'm new to Unity, javascript and so..

    So, I'm having an issue with the rotation of an instance of an prefab.

    My character shoots projectiles, and when it hits something, it's supposed to spawn another prefab at the same position, and the Y rotation parallel to the plane it hits. But with the following code, the prefab spawns at the opposite rotation, perpendicular to the plane...

    Any clues how I can get this to work properly ?
    Thanks
     
    Last edited: Nov 10, 2010
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    what's your issue? Is your splash not oriented properly? The look rotation will align the forward vector of the transform to the supplied direction in this case.
     
  3. TioSlash

    TioSlash

    Joined:
    Nov 10, 2010
    Posts:
    8
    Yeah...it goes the opposite orientation....
     
  4. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    well you can try:

    Code (csharp):
    1. function OnCollisionEnter (collision : Collision)
    2. {
    3. var contact : ContactPoint = collision.contacts[0];
    4. var rot : Quaternion = Quaternion.LookRotation(-contact.normal);
    5. var pos : Vector3 = contact.point;
    6.  
    7. Instantiate(splash, pos, rot);
    8.  
    9. // Destroy the projectile
    10. Destroy (this.gameObject);
    11.  
    12. }
     
  5. TioSlash

    TioSlash

    Joined:
    Nov 10, 2010
    Posts:
    8
    unfortunately it remains the same =(
     
  6. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    what does your splash look like? If the splash is just a plane then its forward vector (blue arrow) should be sticking out of the plane.
     
  7. TioSlash

    TioSlash

    Joined:
    Nov 10, 2010
    Posts:
    8
    Well...I've just solved it, I think....

    The splash looks like a plane, for testing purposes..
    And i've never thought the orientation i used to build the prefab would make a diference....

    You made me realise that....I've just switched the X and Z dimensions and it's all right now
    and i'm laughing a lot at it....a simple sollution to something i've spent almost a hole day to solve =D

    Thanks a lot man !
    Specially for answering so fast
     
  8. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    welcome to the forum