Traditional Culture Encyclopedia - Photography and portraiture - How does unity judge whether an object is in the field of view of the camera?

How does unity judge whether an object is in the field of view of the camera?

There are NPCs running on the screen, which will be deleted after running out of the screen.

The problem now is how to judge that NPC is not in the camera's field of vision.

After searching online, the most common answer is "there are related events in the unity event for reference", but I can't understand it.

Others say that projection to screen coordinates has never been tried. I will try to see the difference between being out of sight and being projected in sight in the future.

Who can tell me a simple judgment method (for example, a function in unity can get O(∩_∩)O~)

Thank you very much

Supplement:

The method of hanging downstairs on NPC can be detected, but after testing, it can only be detected after NPC moves out of the screen for a certain distance.

I tried a method myself, using NPC to map to screen coordinates to judge (compare the width and height of the screen).

Camera. WorldToScreenPoint(enemy . transform . position)

Screen. Width

Screen height

If the mapped coordinates are not in the screen, the screen is O(∩_∩)O~

I occasionally see this method, but I haven't tried it yet. Stick it here first.

A method to judge whether an object is in the field of vision. In fact, the implementation is very simple, just an API method. This method is OnWillRenderObject (), which is called when the game object is visible to the camera in the Unity3D document description.

Let's test the function of this method. First, create a new scene, place a square and a sphere in the scene, and add a light source for easy observation. I use the version of Unity3D5.0beta. When creating a new scene, a light source will be created in the group by default, and other versions should be added by themselves. Next, add a rigid component to the sphere. Running the scene, you will find that the ball will fall under the action of gravity, and the box is still in place.

Create a new script as follows:

Using UnityEngine

Common class detection vision: single behavior (

//Pass in a reference to the box for identification.

Public game object cube;

//Identifies whether the sphere is in the field of view of the camera.

public bool isRendering = true

Private floating-point lasttime = 0;

private float curt time = 0;

Invalid update ()

{

//Compare time records. If they are not equal, it means they are still in the camera's field of vision.

isRendering = curtTime! = Last time? True: false;

if (isRendering)

{

//In the field of vision, the square is red.

Cubes. get component()material . color = color . red;

}

Otherwise {

//Out of view, the square turns white.

Cubes. get component()material . color = color . white;

}

//Pass the current time into the time record of the previous frame.

lastTime = curtTime

}

//Call this method if the object is visible.

void OnWillRenderObject()

{

//Record the start time of the current frame.

curtTime = Time.time

}

}

Drag the script onto the sphere and the box object onto the script. Running the program, we found that the box was red at first.

When the sphere is out of sight, the square turns white, and the effect diagram is as follows: