- Home /
Get the borders of screen
Hi all!
I move my object and I want to prevent out of bounds the screen. I tryed to use Screen.height an Screen.width, but they are return size in pixels: I am mooving my object in points. My moving method:
void Move()
{
var x = gameObject.transform.position.x;
var y = gameObject.transform.position.y;
var z = gameObject.transform.position.z;
if (Input.GetKey(KeyCode.W)) gameObject.transform.position = new Vector3(x, y + speed, z);
if (Input.GetKey(KeyCode.S)) gameObject.transform.position = new Vector3(x, y - speed, z);
if (Input.GetKey(KeyCode.D)) gameObject.transform.position = new Vector3(x - speed, y, z);
if (Input.GetKey(KeyCode.A)) gameObject.transform.position = new Vector3(x + speed, y, z);
}
thx
Answer by porl · Aug 11, 2013 at 05:09 PM
Instead of Screen.height and Screen.width, I think you have to convert the values to world point. Something like:
Vector3 stageDimensions = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height,0));
and then use stageDimensions.x and stageDimensions.y.
it is ScreenToWorldPoint that you need to use. The 3rd float however is the distance from the camera you want to get to know the 3D position from.
Your answer
Follow this Question
Related Questions
Cube Rotating & Moving 1 Answer
getting screen coordinates for 3d object? 1 Answer
Moving object on plane 1 Answer
Is there a function for converting transform to screen coordinates? 1 Answer