Search Unity

How to instantiate an object with the same rotation as prefab's?

Discussion in 'Scripting' started by groch, May 4, 2015.

  1. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Hello there,

    I am making a 2D game, where a Prefab (Prefab1 for example) is destroyed and then it's fragments (Prefab2) are instantiating on it's place. The problem is, when the player destroys Prefab1 while this prefab is rotating, then Prefab2 spawns at default rotation, instead of the same as Prefab1.

    What I should add or change to achieve that?

    My code is:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CollisionHandler : MonoBehaviour {
    5.  
    6.     public GameObject Fragments;
    7.    
    8.     void OnCollisionEnter2D(Collision2D collision) {
    9.         if (collision.gameObject.tag == "dish")
    10.         {
    11.             if (collision.relativeVelocity.magnitude > 5f)
    12.             {
    13.                 DestroyMe ();
    14.             }
    15.         }
    16.  
    17.         GetComponent<AudioSource> ().Play ();
    18.         Vector3 camPos = Camera.main.transform.position;
    19.         if(camPos.y < transform.position.y)
    20.         {
    21.             Camera.main.GetComponent<CameraMover>().targetY = transform.position.y;
    22.         }
    23.        
    24.     }
    25.  
    26.     void DestroyMe()
    27.     {
    28.         Destroy(this.gameObject);
    29.         ShowFragments ();
    30.     }
    31.  
    32.     void ShowFragments()
    33.     {
    34.         GameObject fragments = (GameObject)Instantiate (Fragments);
    35.  
    36.         fragments.transform.position = transform.position;
    37.     }
    38.  
    39. }
    40.  
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    fragments.transform.rotation = transform.rotation
     
    groch and Deleted User like this.
  3. Deleted User

    Deleted User

    Guest

    fragments.transform.rotation = transform.rotation
     
    groch likes this.
  4. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Have you tried to get the transform.rotation before it's destroyed?
     
    groch likes this.
  5. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Thank you for answers, I have tried it and still the same result. :(

    Thank you, can you write me please how it would look like in the script? I am still learning C#.
     
  6. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Try this:

    Code (CSharp):
    1. void ShowFragments()
    2.     {
    3.         GameObject fragments = (GameObject)Instantiate (Fragments);
    4.         fragments.transform.position = transform.position;
    5.         fragments.transform.rotation = transform.rotation;
    6.     }
    If this does not provide the rotation you need, you may need to grab the rotation of the prefab just before it is destroyed by writing another code after the DestroyMe() line.
     
  7. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Thank you, but unfortunately that didn't worked (getting rotation before it's destroyed also).

    Some other ideas?
     
  8. Deleted User

    Deleted User

    Guest

    Did you try this?

    Code (CSharp):
    1. void DestroyMe()
    2.    {
    3. ShowFragments ();
    4.        Destroy(this.gameObject);
    5.    
    6.    }
    7.  
    8.    void ShowFragments()
    9.    {
    10.        GameObject fragments = (GameObject)Instantiate (Fragments);
    11. fragments.transform.rotation = transform.rotation;
    12.         fragments.transform.position = transform.position;
    13.    }
    14.  
    Btw, there is also another Instantiate that takes a position and rotation as arguments.
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Call ShowFragments before Destroy
     
  10. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Ok, I figured it out, it works now. I have changed that "void ShowFragments()" line, now it looks like that:

    Code (CSharp):
    1.     void ShowFragments()
    2.     {
    3.         Instantiate(Fragments, transform.position, transform.rotation);
    4.     }
    5.  
    6.     void DestroyMe()
    7.     {
    8.         Destroy(this.gameObject);
    9.         ShowFragments ();
    10.     }
    Thank you all for your answers. :)
     
    Last edited: May 5, 2015
    n2oukdnb likes this.