- Home /
get screen width and height in world size
Hello everyone,
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)
thank you
Answer by Aladine · Aug 27, 2013 at 02:23 PM
well, am feeling very bad now :p i should never code when i need to sleep, here is what i need :
Vector3 tmpPos = Camera.main.WorldToScreenPoint (transform.position);
if (tmpPos.x > Screen.width ) {
Debug.Log ("Destroy");
Destroy(gameObject);
}
i don't know why i kept asking for screen conversion, thank you all
You might also want a $$anonymous$$imum value check.
if (tmpPos.x > Screen.width
|| tmpPos.x < 0f
|| tmpPos.y > Screen.height
|| tmpPos.y < 0f)
{
...
}
Answer by Ejlersen · Aug 26, 2013 at 09:25 PM
Hm, if you just want to check if the object is visible to the camera, then you can use:
MonoBehaviour.OnBecameVisible() http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnBecameVisible.html
MonoBehaviour.OnBecameInvisible() http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnBecameInvisible.html
You can also use:
GeometryUtility.TestPlanesAABB(...) http://docs.unity3d.com/Documentation/ScriptReference/GeometryUtility.TestPlanesAABB.html
with: GeometryUtility.CalculateFrustumPlanes http://docs.unity3d.com/Documentation/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html
Then you can test the objects bounding box against the camera frustrum.
thank you, but i tried that and it gives me strange results, i did a log check for onBecameVisible and invisible and it print visible when the object is still not in the scene, and "invisible" after the object is far away from the scene, any suggestion ?
OnBecameVisible() and OnBecameInvisible() work with all cameras. In the editor, that includes the scene cameara, so turn your scene camera away from the action if you can and rerun the test. They also test against the Renderer.bounds, so they can return true before an object is visible.
If your game is 2D, you can convert your object to Viewport space (Camera.WorldToViewportPoint(). A position is visible if the x and y values of the returned point are in the range of (0 to 1). You will need to increase the range slightly to account for the width and height of the object.
OnBecameVisible() and OnBecameInvisible() work with all cameras. In the editor, that includes the scene cameara, so turn your scene camera away from the action if you can
i don't know what do you mean by scene camera, i only have one camera, the default one.
and here is what i actually want to do, it kinda "works" but it doesn't give me the right result when i want to apply it to the screen size,
print ("Standard" + myTransform.position.x);
Vector3 worldToScreen = Camera.main.WorldToScreenPoint (myTransform.position);
print ("World" + worldToScreen.x);
Vector3 screenToWorld = Camera.main.ScreenToWorldPoint(worldToScreen);
print ("Screen" + screenToWorld.x );
the ScreenToWorld.x is always equal to myTransform.position.x which is what i want to do (the point is converting screen value to world value)
now when i try this :
Vector3 myScreen = new Vector3(Screen.width,Screen.height,0);
print ("screen"+ myScreen.x);
Vector3 myWorld = Camera.main.ScreenToWorldPoint(myScreen);
print ("myWorld"+ myWorld.x);
the second print always print "0" i tried ScreenToViewPortPoint and the result is always either "1" or "0.9996684" (i track the value by changing the game window size during the play) thank you
When in the Unity editor, you have two cameras. The "Ingame $$anonymous$$ain Camera" and the "Editor Scene Camera" (the one you use to edit your scene with).
When using ScreenToWorldPoint, remember that the z-value is distance from camera. So giving a distance of 0 will likely give something resembling the cameras own position.
You could calculate the distance to your object, e.g. (obj.transform.position - Camera.main.transform.position).magnitude.
Answer by twobob · Sep 29, 2014 at 11:20 PM
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
Debug.DrawLine(trajectoryStartPoint, trajectoryEndPoint, Color.red);
Inside the helper occlusion check. (pictured, with some extra GUI texts for clarity)
Just to add. if one really wanted to use this for probing occlusion, a 5th "$$anonymous$$IDDLE" probe with some extra depth would be helpful.
// $$anonymous$$IDDLE
p = theCamera.ScreenToWorldPoint (
new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, theCamera.nearClipPlane)
);
// trajectoryEndPoint = p;
trajectory = trajectoryStartPoint - p;
float distanceApart = Vector3.Distance (player.transform.position,p);
trajectoryEndPoint = trajectoryStartPoint + -trajectory * 1.05f ;
Debug.DrawLine (trajectoryStartPoint, trajectoryEndPoint, Color.red);
// Check for hit.
if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
//Vector3 targetPosition = hit.point;
Debug.Log("Occluded Center screen");
}