• 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 safak93 · May 05, 2016 at 05:23 PM · camerainvisible

Detect from which side of camera object is out of screen

Hi,

First of all it's a 2D game. How can I detect from which side of the camera the object became invisible? Currently I'm using void OnBecameInvisible() but with them I can handle only one side when the object leaves it from the left side. Of course it doesn't work when the player moves back and the object leaves the screen from right side.

Here is the code that I'm using right now:

 public float offset = 5;
 
 void OnBecameInvisible() {
     transform.position = new Vector3(Camera.main.ViewportToWorldPoint(Vector3.one).x + offset, transform.position.y, transform.position.z);
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by callen · May 05, 2016 at 05:35 PM

I'd never heard of this function before, that's very useful!

If it's a 2D game, and youre only interested in the left and right sides of the screen this should work (you can modify it to do all 4 sides of the screen too, with a bit of thought about how corners are handled).

So the thing you're asking for is basically this:

 bool exitedLeft = Camera.main.transform.position.x > transform.position.x;

But I'm not totally sure what you're trying to do. It looks like clamp the object to remain on-screen? If so you could modify like this:

      public float offset = 5;
      
      void OnBecameInvisible() {
         bool exitedLeft = Camera.main.transform.position.x > transform.position.x;
         float side = exitedLeft ? 0 : 1, sideOffset = exitedLeft ? offset : -offset;
         float posX = Camera.main.ViewportToWorldPoint(new Vector3(side, 1, 1)).x + sideOffset;
         transform.position = new Vector3(posX, transform.position.y, transform.position.z);
      }
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 safak93 · May 05, 2016 at 06:02 PM 0
Share

I made a little gif what I want to try. The black rectangle is the camera and the other one is the object from background. https://giphy.com/gifs/xT4uQ9tz1Omk5gXJYY

https://giphy.com/gifs/xT4uQgHJBRlfPlVrWw

avatar image
0

Answer by Ryanless · May 05, 2016 at 11:47 PM

make a box with boundary.

from a other project i had:

 [System.Serializable]
 public class Boundary
 {
     public float xMin,xMax, zMin, zMax;
 }

and the code:

         Vector3 temp = transform.position;
         if (temp.x > boudary.xMax + 0.5f) {
             temp.x = boudary.xMin -0.5f ;
         } else if (temp.x < boudary.xMin - 0.5f) {
             temp.x = boudary.xMax + 0.5f ;
         }
 // at the end
 transform.position = temp;

so if the object is more than 0.5 out of the x boundary it goes to the other side. In my case the boundary where static. Just make them dynamiclly ajust to your camera and then your done.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

51 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

Related Questions

Camera not showing in scene view 0 Answers

Nothing Visible in Game View 1 Answer

Make an object invisible from 1 camera 1 Answer

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

how can I Make my player model invisible to my camera 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