- Home /
 
If the player is behind the camera destroy it.
So I'm making a game where you are a grenade trying to cross a road without getting hit by any cars. So the camera is constantly moving right so you cant stay in the same spot for too long so I want it so that if the camera passes the player you lose (just destroy the player). I don't want to use (Renderer.IsVisible == false) because if I am under a car and the camera cant see the player then I will lose. Sorry if bad explination :D
Any help is appreciated!
Game so far: 
Have you actually tested (Renderer.isVisible) when the player is behind or under a car? I just tried that and as long as the renderer is inside the field of view of the camera, it doesn't get blocked by other gameobjects, at least not in my test. 
Also, does your grenade have just left and right movement? If so, just use a simple box collider at the limit.
Answer by tadadosi · Jun 19, 2020 at 04:32 PM
You could still use (!Renderer.isVisible) and simply use a series of enums and a switch to determine which renderer should be used to check if isVisible. Also, save yourself some time and don't use (yourBool == true), you can simply write (yourBool) for true and (!yourBool) for false.
Here is a working example of what I'm proposing. 
 using UnityEngine;
 
 public class DestroyWhenNotVisible : MonoBehaviour
 {
     public enum PlayerIsOn { Foot, Car, Airplane }
     public PlayerIsOn playerIsOn;
 
     public Renderer airplaneRenderer;
     public Renderer carRenderer;
     public Renderer playerRenderer;
 
     private bool isReady;
 
     private void Start()
     {
         // Had to use this dirty Invoke cuz isVisible was being called right from the start.
         Invoke("GetReady", 0.1f);
     }
 
     private void Update()
     {
         if (isReady)
         {
             switch (playerIsOn)
             {
                 case PlayerIsOn.Foot:
                     if (!playerRenderer.isVisible)
                     {
                         Debug.Log("Player not Visible");
                     }
                     break;
 
                 case PlayerIsOn.Car:
                     if (!carRenderer.isVisible)
                     {
                         Debug.Log("Car not Visible");
                     }
                     break;
 
                 case PlayerIsOn.Airplane:
                     if (!airplaneRenderer.isVisible)
                     {
                         Debug.Log("Airplane not Visible");
                     }
                     break;
 
                 default:
                     break;
             }
         }
     }
 
     private void GetReady()
     {
         Debug.Log("Ready");
         isReady = true;
     }
 }
 
               I hope it can give you an idea to accomplish what you are looking for. Best of luck!
Thanks you very much for ur answer! I will try isvisible again and let u know what happens :)
So I said '`if (!m_Renderer.isVisible) { Destroy(gameObject); }`
It just instantly destroys the grenade for some reason. Any idea?
If you simply added that to your Update method without adding the isReady bool I used, it will destroy the gameobject as soon as it starts, I'm not sure why, but I had to do that tricky part to stop it from happening. 
Ah ok. Also if the grenade is under a car and not visible to the camera would it get destroyed?
Answer by nosmokingbandit · Jun 19, 2020 at 02:44 AM
If this is 2D with an orthographic camera you can put a trigger collider just out of view on the left side of the camera and the player object can destroy itself when entering this trigger. Make is a component or child of the camera and it will always stay just outside of the frame.
Hey! So my game is not 2d it is 3d. I have thought of something like that but i thought there might be a more accurate way to do it than that. Thanks
Answer by SiniKettu_ · Jun 19, 2020 at 09:41 AM
try something like this ?
 float myFloat = //set a value which fits well;
         if(thePlayer.transform.position.x + myFloat < Camera.main.transform.position.x)
         {
 
         }
 
               So if the player goes too far to the left of the camera, he loses !
You can also use renderer.isVisible to check if the player is still on the screen. 
I assume that the camera look at the road from the top. I also assume that the player and the camera go from left to right following the X axis, so when the game plays, the x coordinates of both increase.
But when the difference between these coordinates becomes too large (= greater than a value that you have chosen) the player fails.
So for each frame you can do this :
 float loseTreshold = whenever you want
 
 if($$anonymous$$athf.Abs(thePlayer.transform.position.x - camera.main.transform.position.x)) > loseTreshold)
 {
      //game over
 }
                  I updated the question to show a pic of the game.
Your answer
 
             Follow this Question
Related Questions
Rotating player relative to the camera (Unity C#) 1 Answer
Use OpenCV with in-game camera to detect road lanes 0 Answers
rotate the camera in the z axis 1 Answer
Unity Deleted My Game 1 Answer
Reverse Camera Twitches 1 Answer