Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Patrick96kelly · Apr 12, 2016 at 06:40 PM · cameradestroychange

Changing camera after being destroyed

Hey, I am making a 3rd person shooter game where the main camera is a child of the player game object. I have this so that the camera will move whenever the player moves and will turn whenever the player turns. However, I have been trying to set it up so that when you die, the camera ("MainCamera") will swap to another camera called "GameOverCamera". The reason for this is that because the camera is a child of the player object, when the player gets destroyed, so does the camera and it then displays a black screen saying "Scene is missing a fullscreen camera". Currently this is my script and I don't know how to do this:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerHealth : MonoBehaviour
 {
     public float m_StartingHealth = 100f;               // The amount of health each tank starts with.
     public Slider m_Slider;                             // The slider to represent how much health the tank currently has.
     public Image m_FillImage;                           // The image component of the slider.
     public Color m_FullHealthColor = Color.green;       // The color the health bar will be when on full health.
     public Color m_ZeroHealthColor = Color.red;         // The color the health bar will be when on no health.
     public GameObject m_ExplosionPrefab;                // A prefab that will be instantiated in Awake, then used whenever the tank dies.
     public Camera mainCamera;
     public Camera gameOverCamera;
 
     private AudioSource m_ExplosionAudio;               // The audio source to play when the tank explodes.
     private ParticleSystem m_ExplosionParticles;        // The particle system the will play when the tank is destroyed.
     private float m_CurrentHealth;                      // How much health the tank currently has.
     private bool m_Dead;                                // Has the tank been reduced beyond zero health yet?
     
 
     private void Awake()
     {
         // Instantiate the explosion prefab and get a reference to the particle system on it.
         m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
 
         // Get a reference to the audio source on the instantiated prefab.
         m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>();
 
         // Disable the prefab so it can be activated when it's required.
         m_ExplosionParticles.gameObject.SetActive(false);
     }
 
 
     private void OnEnable()
     {
         // When the tank is enabled, reset the tank's health and whether or not it's dead.
         m_CurrentHealth = m_StartingHealth;
         m_Dead = false;
 
         // Update the health slider's value and color.
         SetHealthUI();
     }
 
 
     public void TakeDamage(float amount)
     {
         // Reduce current health by the amount of damage done.
         m_CurrentHealth -= amount;
 
         // Change the UI elements appropriately.
         SetHealthUI();
 
         // If the current health is at or below zero and it has not yet been registered, call OnDeath.
         if (m_CurrentHealth <= 0f && !m_Dead)
         {
 
             mainCamera.enabled = true;
             gameOverCamera.enabled = false;
 
             OnDeath();
         }
 
     }
 
 
     private void SetHealthUI()
     {
         // Set the slider's value appropriately.
         m_Slider.value = m_CurrentHealth;
 
         // Interpolate the color of the bar between the choosen colours based on the current percentage of the starting health.
         m_FillImage.color = Color.Lerp(m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
     }
 
 
     private void OnDeath()
     {
         // Set the flag so that this function is only called once.
         m_Dead = true;
 
         // Move the instantiated explosion prefab to the tank's position and turn it on.
         m_ExplosionParticles.transform.position = transform.position;
         m_ExplosionParticles.gameObject.SetActive(true);
 
         // Play the particle system of the tank exploding.
         m_ExplosionParticles.Play();
 
         // Play the tank explosion sound effect.
         m_ExplosionAudio.Play();
 
         // Turn the tank off.
         gameObject.SetActive(false);
     }
 }

I know it may sound like a stupid question but I really don't know what to do. Thanks.

Comment
Add comment
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

1 Reply

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

Answer by Winterblood · Apr 12, 2016 at 06:45 PM

Set the Depth value on your player's camera to 1. Create your GameOverCamera pointing at a nice view of your level, and leave its Depth at 0. The higher Depth will take priority over the lower one, but when destroyed the level view will be revealed.

Comment
Add comment · Show 1 · 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 Patrick96kelly · Apr 12, 2016 at 07:07 PM 0
Share

Thanks, I actually got it working in the end, thanks for the quick response!

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change position - cameras - slider 1 Answer

How can I change camera when colliding with "X" object? 1 Answer

Can I make the camera flip upside-down when spacebar is pressed? 3 Answers

Changing camera with different culling mask 1 Answer

Sliding between camera change 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges