- Home /
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.
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 ?
What exactly are you trying to accomplish (end result)?
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.
$$anonymous$$ay you explain this script? I can not use it correct for my project.
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"
@Taylor-Libonati How to compensate for rotation? Say the camera rotates the object out of view, this seems to behave differently
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.
Post here. Thank you for the quick response. The code functions differently when rotating a gameObject out of the cameras FOV
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.
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.
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.
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 ?
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.
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?
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?
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 )
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.
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.