Transforms position
from world space into viewport space.
// Finds out whether target
is on the left or right side of the screen
var target : Transform;
function Update () {
var viewPos : Vector3 = camera.WorldToViewportPoint (target.position);
// viewport coordinates range from zero to one
if( viewPos.x > 0.5 )
print ("target is on the right side!");
else
print ("target is on the left side!");
}
using UnityEngine; using System.Collections; public class Example : MonoBehaviour { public Transform target; void Update() { Vector3 viewPos = camera.WorldToViewportPoint(target.position); if (viewPos.x > 0.5F) print("target is on the right side!"); else print("target is on the left side!"); } }
import UnityEngine import System.Collections public class Example(MonoBehaviour): public target as Transform def Update() as void: viewPos as Vector3 = camera.WorldToViewportPoint(target.position) if viewPos.x > 0.5F: print('target is on the right side!') else: print('target is on the left side!')