please i am creating a 3D runner game, And i have attached the camera to the player as a child, but whenever my player gets destroyed the camera is destroyed too, i was wondering if there is a way whereby my player would be destroyed but leaving my camera intact.
hi;
just where u are Destroying your player u can Remove camera parent so it wont be a child of player any more :
Camera.main.transform.parent = null;
the code above works if u have only one camera in the scene if not then in your script make a refrence to camera object then do it like that ;
You have to Destroy the script first and after the gameObject with a bit of delay.
private void Start()
{
Destroy(this);
Destroy(gameObject, 0.1f);
}
private void OnEnable()
{
Camera.main.transform.SetParent(transform);
}
private void OnDestroy()
{
Camera.main.transform.parent = null;
}
or if nothing else is a child of the Player
private void Update()
{
if (Input.GetKeyDown(KeyCode.D))
{
transform.DetachChildren();
Destroy(gameObject);
}
}