• 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
8
Question by Tony_T · Jun 03, 2014 at 11:45 AM · cameravisiblefieldofview

If game object is in camera's field of view

Hello, I've been trying to create a script that will turn true once a specific game object is in a camera's field of view but I just can't get it working right. I want the script to turn true only when the selected game object is within the field of view of the camera that holds the script. Check the picture if that doesn't help.

alt text

I tested some script like OnWillRenderObject(), Renderer.isVisible, Renderer.OnBecameVisible, and OnBecameInvisible but nothing seems to works. Maybe something like that:

 //This script should be attached to the camera
 var Target : GameObject;
 
 function Update ()
 {
    if (Target.renderer.isVisible)
    print ("Visible");
    
    else
    print ("Not Visible");
 }

But it still doesn't seem to work, it is visible no mater what because the isVisble command works even if it's visible by any camera but it's considered visible when it needs to be rendered in the scene. Any help ?

example.jpg (228.5 kB)
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 cryingwolf85 · Jun 03, 2014 at 03:15 PM 0
Share

What exactly are you trying to accomplish (end result)?

3 Replies

· Add your reply
  • Sort: 
avatar image
34

Answer by Taylor-Libonati · Aug 18, 2015 at 10:14 PM

Here's the method I used with success:

Convert the position of the object into viewport space. If the viewport space is within 0-1 you are in the cameras frustum. This method also allows you to add a margin amount if you want, so that objects turn on when they are almost in view.

 Vector3 screenPoint = playerHead.leftCamera.WorldToViewportPoint(targetPoint.position);
 bool onScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;

EDIT: @tanoshimi's answer is actually a cool alternative. It uses an extension method to give a function to all renderers. http://wiki.unity3d.com/index.php?title=IsVisibleFrom Just add CameraExtensions.cs to your project and now from any renderer you can call IsVisibleFrom as shown in the example. Not sure if its faster or slower than my solution.

Comment
Add comment · Show 14 · 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 baroquedub · Mar 14, 2016 at 08:48 PM 0
Share

Works great for me. Thank you

avatar image Alexei_UA · Mar 19, 2017 at 05:16 PM 0
Share

$$anonymous$$ay you explain this script? I can not use it correct for my project.

avatar image Taylor-Libonati Alexei_UA · Jun 05, 2017 at 04:32 PM 1
Share

targetPoint is the transform that I want to test and see if it is in the cameras view. You should replace this with the transform you want to test.

playerHead.leftCamera is the camera that you are checking the object against. You should replace this with the camera you want to use.

onScreen is a variable that is true when "targetPoint" is in view of "playerHead.leftcamera"

avatar image jvhgamer · Dec 05, 2017 at 09:33 PM 0
Share

@Taylor-Libonati How to compensate for rotation? Say the camera rotates the object out of view, this seems to behave differently

avatar image Taylor-Libonati jvhgamer · Dec 05, 2017 at 09:55 PM 1
Share

Screen point is the position relative to the cameras transform and view. Which includes the rotation of the camera. So the camera or the object rotating should not effect anything.

$$anonymous$$ake sure that you are checking the screenPoints Z property. If you are only checking x and y you will get a TRUE when the object is behind the camera.

If you have a specific implementation that isn't working I suggest making a new question. You can even link me to it if you want.

avatar image jvhgamer Taylor-Libonati · Dec 05, 2017 at 10:37 PM 0
Share

Post here. Thank you for the quick response. The code functions differently when rotating a gameObject out of the cameras FOV

avatar image impurekind · Aug 26, 2018 at 04:17 PM 0
Share

And how exactly do you "add CameraExtensions.cs" to your project?

It's great telling us we have to do this to get the project to work, because that's the part I was missing, but now I'm missing the part on how I actually do this.

Same goes for your original suggestion too: I'm sure it works but you have not said what I'm supposed to do to set up and get stuff like "playerHead" and "targetPoint" to not come up with the little red squiggly error lines indicating something is wrong or missing when I try to use your code in my script.

All it takes is for the people giving us these solutions to fully give us the complete solution and all the parts we actually need to get it to work.

avatar image Taylor-Libonati impurekind · Aug 27, 2018 at 06:59 PM 2
Share

For future reference, answers on this forum are for the questions being asked. No one is going to walk through every little detail unless that is part of the question. If you need more help than what is given you should post a new question.

If you are having a hard time adding files or understanding how code works I suggest watching some tutorials on unity scripting and posting further questions on this site.

In this case you can replace playerHead.leftCamera with your own "Camera" variable (You can use Camera.main if that is your only camera). And targetPoint should be your own "Transform" variable (you can use "this.transform" if this script is attached to the object you want to test)

And "add CameraExtensions.cs" means create a file in your project and name it CameraExtensions.cs. You can do that in your file browser or you can right click a folder in Unity and hit Create C# script.

avatar image impurekind Taylor-Libonati · Aug 27, 2018 at 11:47 PM 0
Share

But you see how that's a lot of stuff just to leave for people to work out on their own, right?

I mean, like many people, I'm going online and doing searches to find solutions for the multitude of issues that crop up when using something like Unity, and my search in relation to my issue around how to detect if the player is looking at something led me here.

So, I try to use the solution provided but it's not really a complete solution. And that's happening time and time again when I find all these advice forums and stuff online, and even with Unity's own official documentation, which also rarely gives enough info on how to actually properly implement much of the stuff it goes on about outside of a single example.

It just takes a couple of notes saying things like "replace this part here with your own variable name" and "make sure to create a script in your project and give it this name and attach it to the object you want to use it on" and stuff like that. And this is all just stuff directly related to the solution as provided.

If you've ever seen the amount of times someone has written something along the lines of "I've been trying for a week looking online to sort this issue but haven't found anything useful" on these various help forums then you'll understand that people just need proper solutions and not a week of wandering aimlessly finding stuff that maybe helps but doesn't really unless you kinda already know what you're doing anyway, in which case you kinda could just solve the problem yourself already.

If the idea of these forums is to provide help for people's issues and problems then all I expect it for that help to be genuinely helpful rather than just as confusing and frustrating as not knowing what to do in the first place--that's all.

Show more comments
avatar image
6

Answer by tanoshimi · Jun 03, 2014 at 11:51 AM

The problem with OnWillRenderObject, Renderer.isVisible etc. is that they test whether an object is visible from any camera, and that includes the editor camera used to render the scene view.

Have you tried http://wiki.unity3d.com/index.php?title=IsVisibleFrom ?

Comment
Add comment · Show 9 · 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 Tony_T · Jun 03, 2014 at 12:02 PM 0
Share

This is a little confusing for me. I'm not familiar with C#. Also i want to be able to choose the object that is in the camera's field of view with ether a variable or tag.

avatar image Tony_T · Jun 03, 2014 at 02:48 PM 0
Share

It is possible to work something out with Vector3.Angle ?

avatar image tanoshimi · Jun 03, 2014 at 04:14 PM 0
Share

You can choose the object and the camera from which you're testing. And there's code in JS as well as C#. Did you try scrolling down that page?

avatar image impurekind tanoshimi · Aug 26, 2018 at 04:08 PM 0
Share

It does not work, at least not as is written on that page specifically (it just has that little red squiggly line and a help message that says "renderer does not contain a definition for IsVisibleFrom and . . . ". So, if something other than the code they have supplied is required to get it to work then it's not made clear at all to me on that wiki page what extra thing it it that is required, be it adding a using directive or setting up a variable at the start or whatever, and how to do it.

So, do you have a suggestion for how to get that code to actually work?

avatar image Hellium impurekind · Aug 26, 2018 at 04:19 PM 0
Share

The code in the page uses the old getters. You have to use this ins$$anonymous$$d :

 if (GetComponent<Renderer>().IsVisibleFrom(Camera.main)) Debug.Log("Visible");
 else Debug.Log("Not visible");

(A little search on Google would have given you this question )

Show more comments
avatar image Tony_T · Jun 03, 2014 at 04:28 PM 0
Share

I saw it but I'm not sure how it works.

avatar image
1

Answer by rich_seabirdsim · Sep 26, 2014 at 02:42 AM

A bit late for this answer, but all you really need is to compute the angle between transform.forward of the camera and the vector to your object, which is derived by subtracting the transform.position (s). You can use Vector3.angle for that. If the resultant angle is less than half the field of view, than your object will be in the field of view.

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 Taylor-Libonati · Jun 07, 2017 at 04:30 PM 0
Share

Correct me if I am wrong, but wouldn't this only work with square camera resolutions? If you are comparing it against the field of view, then that will only be the vertical or horizontal field of view, not both. But as long as you used the larger of the fields of view it should be a good estimate.

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

31 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

Related Questions

Camera movement in a 2D? (like angry birds) 1 Answer

How to know if a UI element is currently seen on the screen in Camera-Worldspace mode 0 Answers

GUI Texture not visible 3 Answers

Indicate with markers the direction of Gameobjects which are outside of the camerafield. 0 Answers

How can I turn off gameobjects icons in camera view? 0 Answers


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