Position in different screen sizes
Hi everybody. In my game a have a 2D block which moves forward and backward horizontally. When the 2D block reach the edge of the screen it change it direction and move to the other side and vice versa.
if (transform.position.x >= 27.3)
{
speed = speed * -1;
}
if (transform.position.x; <= -6.99)
{
speed = speed * -1;
} GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);
Now the problem is that not all mobiles have the same screen size so in some cases the block might go outside of the screen then change it direction and in other cases the block might change it direction before it reach the edge of the screen. So what can i do to solve this problem
Thanks
Answer by Dave-Carlile · Oct 14, 2015 at 03:58 PM
You can use the Screen class to get information about the screen dimensions.
The Screen class has properties called width
and height
that return the current width and height of the screen. Those can be used to detect when your object is at the edge of the screen.
so you are saying that i should use an if statement that specify the location i want for each different scree resolution.
Your answer
Follow this Question
Related Questions
BCE0019: 'position' is not a member of 'Object'. 0 Answers
Keep the players position constant between two different levels 1 Answer
Set cursor position at the middle of the screen. 1 Answer
Kinect joint position co-ordinates script 0 Answers
if transform.position == 5 Change the Scale of game object 1 Answer