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

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:

using UnityEngine;
using System.Collections;

public class CollisionHandler : MonoBehaviour {

    public GameObject Fragments;
   
    void OnCollisionEnter2D(Collision2D collision) {
        if (collision.gameObject.tag == "dish")
        {
            if (collision.relativeVelocity.magnitude > 5f)
            {
                DestroyMe ();
            }
        }

        GetComponent<AudioSource> ().Play ();
        Vector3 camPos = Camera.main.transform.position;
        if(camPos.y < transform.position.y)
        {
            Camera.main.GetComponent<CameraMover>().targetY = transform.position.y;
        }
       
    }

    void DestroyMe()
    {
        Destroy(this.gameObject);
        ShowFragments ();
    }

    void ShowFragments()
    {
        GameObject fragments = (GameObject)Instantiate (Fragments);

        fragments.transform.position = transform.position;
    }

}

fragments.transform.rotation = transform.rotation

2 Likes

fragments.transform.rotation = transform.rotation

1 Like

Have you tried to get the transform.rotation before it’s destroyed?

1 Like

Thank you for answers, I have tried it and still the same result. :frowning:

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

Try this:

void ShowFragments()
    {
        GameObject fragments = (GameObject)Instantiate (Fragments);
        fragments.transform.position = transform.position;
        fragments.transform.rotation = transform.rotation;
    }

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.

Thank you, but unfortunately that didn’t worked (getting rotation before it’s destroyed also).

Some other ideas?

Did you try this?

void DestroyMe()
   {
ShowFragments ();
       Destroy(this.gameObject);
    
   }

   void ShowFragments()
   {
       GameObject fragments = (GameObject)Instantiate (Fragments);
fragments.transform.rotation = transform.rotation;
        fragments.transform.position = transform.position;
   }

Btw, there is also another Instantiate that takes a position and rotation as arguments.

Call ShowFragments before Destroy

Ok, I figured it out, it works now. I have changed that “void ShowFragments()” line, now it looks like that:

    void ShowFragments()
    {
        Instantiate(Fragments, transform.position, transform.rotation);
    }

    void DestroyMe()
    {
        Destroy(this.gameObject);
        ShowFragments ();
    }

Thank you all for your answers. :slight_smile:

1 Like