if i have a game with a resolution 800x600, how to make a condition to check if some object is out of the screen ? in other term i want to convert the 800 and 600 to the transform unit (basically it’s always a small number) i tried doing this :
print (Camera.main.ScreenToWorldPoint(new Vector3(800,600,0)));
but the result is always : (0.0, -0.1, -2.8)
using UnityEngine;
using System.Collections;
/// <summary>
/// Update camera near plane world coords.
/// This could be repurposed to handle camera occlusion
/// Directing some event (such as moving the camera) to occur
/// rather than the debug messages.
/// </summary>
public class UpdateCameraNearPlaneWorldCoords : MonoBehaviour {
// properties
public Camera theCamera;
public GameObject player;
Vector3 checkpoint; // player transfrom that we offset from
Bounds bounds; // player bounds for positioning sensible check point.
Vector3 p; // point in space
Vector3 trajectoryEndPoint; // where we want to check we can see in world space
Vector3 trajectoryStartPoint; // The "checkpoint" with a useful offset.
Vector3 trajectory; // the direction of the ray to fire. ( I invert it here to be fair)
static RaycastHit hit; // Stop it needing to be declared a bazzillion times.
// Helper Method
static bool CheckIsOccluded (Vector3 trajectoryStartPoint, Vector3 trajectory, Vector3 trajectoryEndPoint)
{
// Simply return true or false. True means we are occluded.
// We could have used...
//return Physics.Raycast (trajectoryStartPoint, -trajectory, out hit, Vector3.Distance (trajectoryStartPoint, trajectoryEndPoint));
//but this makes more sense in this instance
return Physics.Linecast(trajectoryStartPoint, trajectoryEndPoint, out hit);
}
// Use this for initialization if you are networked or something
void Start () {
// Could find objects dynamically is we are running NetworkView stuff. via isMine.
// In this example The PLAYER and CAMERA are simply dragged on to get the reference
// You might have a mesh filter or w/e - get the right one from the right level (Self/Child).
bounds = player.transform.gameObject.GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh.bounds;
}
void SetupValuesReadyForCornerCheck (float x, float y)
{
p = theCamera.ScreenToWorldPoint (new Vector3 (x, y, theCamera.nearClipPlane));
trajectoryEndPoint = p;
trajectory = trajectoryStartPoint - trajectoryEndPoint;
Debug.DrawLine (trajectoryStartPoint, trajectoryEndPoint, Color.red);
}
void Update () {
checkpoint = player.transform.position;
// Make this be somewhere useful to you. This place is about right for me.
trajectoryStartPoint = (checkpoint - bounds.center) + (Vector3.up *0.4f) ;
// TOP LEFT
SetupValuesReadyForCornerCheck (0, Screen.height);
if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
//Vector3 targetPosition = hit.point;
Debug.Log("Occluded Top Left");
}
// BOTTOM LEFT
SetupValuesReadyForCornerCheck (0, 0);
// Check for hit.
if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
//Vector3 targetPosition = hit.point;
Debug.Log ("Occluded Bottom Left");
}
// TOP RIGHT
SetupValuesReadyForCornerCheck (Screen.width, Screen.height);
// Check for hit.
if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
Debug.Log("Occluded Top Right");
}
// BOTTOM RIGHT
SetupValuesReadyForCornerCheck (Screen.width,0);
// Check for hit.
if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
//Vector3 targetPosition = hit.point;
Debug.Log("Occluded Bottom Right");
}
}
}
Is not far off the mark. Could doubtless be more efficient.
EDIT:
if you want to see what is happening drop in a