Hi I am having an object in the world. My camera is perspective. I want to get the screen (Vector2) position of the object at the moment at it goes out of the camera’s field of view?? Just like in fruit ninja when the fruit is missed a cross sign is displayed, similarly I want to display a sprite at that position… Any idea?
There are multiple different ways to accomplish your goal, with strengths and weaknesses to each. Perhaps the easiest is to use OnBecomeInvisible(). This function will be called when no camera sees an object. Note this includes the Scene camera, so when testing in the editor, place your scene camera so that either it is looking away from the objects or sees less than your main camera. If you don’t it will fail in the editor (but will work fine in your built game). As for placing the label, assuming your labels are fixed size and placed on the same ‘Z’ position relative to the camera, you can use viewport coordinates. Viewport coordinates go from (0,0) in the lower left to (1,1) in the upper right. Any position with a value below 0 or above 1 for either x or y will not be visible. As a bonus, GUITextures and GUIText live in viewport space. I don’t know how you have your game structured, but here is a bit of example code:
#pragma strict
var guitexture : Transform;
function OnBecameInvisible () {
var viewportPos = Camera.main.WorldToViewportPoint(transform.position);
viewportPos.x = Mathf.Clamp(viewportPos.x, 0.1, 0.9);
viewportPos.y = Mathf.Clamp(viewportPos.y, 0.1, 0.9);
guitexture.position = viewportPos;
}