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;
}
}
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.