• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Max 4 · Apr 19, 2011 at 10:50 AM · cameradestroy

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image poncho · Apr 19, 2011 at 01:16 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Max 4 · Apr 20, 2011 at 06:45 AM 0
Share

It says "Assets/aeroplane/crash.js(6,21): BCE0019: 'parent' is not a member of 'UnityEngine.Camera'."

avatar image Owen-Reynolds · Apr 20, 2011 at 08:05 PM 0
Share

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.

avatar image Max 4 · Apr 21, 2011 at 12:32 AM 0
Share

It still crashes. The explosion begins, and then the game stops running.

avatar image Owen-Reynolds · Apr 21, 2011 at 02:39 AM 0
Share

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

avatar image
0

Answer by Marowi · 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
}
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Marowi · Apr 19, 2011 at 10:53 AM 0
Share

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.

avatar image Max 4 · Apr 19, 2011 at 10:53 AM 0
Share

OK, so there is no camera code, it's simply dragged into position and placed in the players heirachy.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to make camera position relative to a specific target. 1 Answer

How can I select, single out and zoom into my object? 2 Answers

Destroy object question! 3 Answers

how to destroy camera instatiated from prefab? 0 Answers

Flickering Shadows and Z-Fighting with camera low near clipping 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges