- Home /
Destroying the object but leaving the camera
I have a game where the camera is locked onto the player. The player is destroyed upon impact with an object, and explodes. However, the game crashes as the camera is also destroyed. Is there any way I could leave the camera to view the explosion?
it would be really of help if you say your code to the destroy, wich class you use to call it and wich method is in the destruction
Answer by Owen-Reynolds · Apr 19, 2011 at 01:52 PM
Destroying an object destroys all the kids. That's a really nice feature (killing a bullet also kills the con-trail, for example.) In your case, it destroys the camera. The real solution is you will naturally add a camera script, eventually. and will unchild the camera. If you don't want to do that yet, try:
// player killing itself code:
Camera.main.transform.parent=null; // unchild from player. Keeps same position in world
Destroy(gameObject); // player kills itself
If you spawn a new player, you'll need to reposition the camera:
newPlayer = Instantiate(blah, blah...) Camera.main.transform.parent = newPlayer; // locked onto new player // keeps old pos and facing (where last guy died,) but will now move with player
// No way camera can know to snap to correct position, so make it: Camera.main.transform = new Vector3(0,4,-10); // Untested. since cam is now a child, puts it relative to player: up and behind
Camera.main.transform.LookAt(newPlayer); // Fix the angle. Camera should stay "locked" at this relative angle
It says "Assets/aeroplane/crash.js(6,21): BCE0019: 'parent' is not a member of 'UnityEngine.Camera'."
Right...Camera is only a Component, a part of something. You look at the script reference, and Camera's don't have parents. But, we want to find the parent. What does have a parent? Well, the actual camera, in the Inspector, hangs off a transform (everything deos,) and Camera.transform is legal, and transforms do have parents. Since 2 checked me, editting answer.
It still crashes. The explosion begins, and then the game stops running.
Try editing your Q with details. Uh...no camera code, childed to player(?) Show the code that destroys you, does it give any errors? Does it freeze? All Sky? Drop out of Play mode? If you unchild the camera and put it in the sky, does it still break? etc...
Answer by · Apr 19, 2011 at 10:52 AM
Add an ifcheck to your camera code, so it will only track the player if they exist.
Pseudocode:
if ( player != null )
{
// Do your camera 'locking' here
}
To clarify, your game is almost certainly 'crashing' when you destroy the player because it's trying to look at an object that no longer exists. By adding this ifcheck, you avoid the crash when the player has been destroyed, because the code won't be executed.
O$$anonymous$$, so there is no camera code, it's simply dragged into position and placed in the players heirachy.
Answer by poncho · Apr 19, 2011 at 01:14 PM
when using the destroy method make sure to dont use the camera
if(Objecttodestroy != GameObject.Find(Camera))
{
GameObject.Destroy(Objecttodestroy);
}
or in the way you are destroying your objects
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroy object question! 3 Answers
Changing camera after being destroyed 1 Answer
How can I make the Player be destroyed when I touch the end of the camera? 1 Answer
How to destroy a projectile when it leaves any side of the camera view on a 2D game? 1 Answer