Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mahmouds · Jun 18, 2020 at 08:40 PM · unity 5cameradestroycar

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: alt text

unity-pic.png (362.8 kB)
Comment
Add comment · Show 2
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 tadadosi · Jun 19, 2020 at 06:19 PM 0
Share

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.

avatar image tadadosi · Jun 19, 2020 at 06:23 PM 0
Share

Also, does your grenade have just left and right movement? If so, just use a simple box collider at the limit.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 7 · 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 mahmouds · Jun 19, 2020 at 06:13 PM 0
Share

Thanks you very much for ur answer! I will try isvisible again and let u know what happens :)

avatar image mahmouds · Jun 19, 2020 at 06:24 PM 0
Share

So I said '`if (!m_Renderer.isVisible) { Destroy(gameObject); }`

It just instantly destroys the grenade for some reason. Any idea?

avatar image tadadosi mahmouds · Jun 19, 2020 at 06:26 PM 0
Share

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.

avatar image mahmouds tadadosi · Jun 19, 2020 at 06:48 PM 0
Share

Ah ok. Also if the grenade is under a car and not visible to the camera would it get destroyed?

Show more comments
avatar image
0

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.

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 mahmouds · Jun 19, 2020 at 03:40 PM 0
Share

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

avatar image
0

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.

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 mahmouds · Jun 19, 2020 at 03:41 PM 0
Share

$$anonymous$$y game is 3d so would this still work?

avatar image SiniKettu_ mahmouds · Jun 19, 2020 at 04:26 PM 0
Share

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
 }

avatar image mahmouds SiniKettu_ · Jun 19, 2020 at 06:12 PM 0
Share

I updated the question to show a pic of the game.

Show more comments

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

286 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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