Now i have this script:
private void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
if(horizontal > 0 && transform.position.x < 2.53 && gameController.started)
{
transform.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
}
if (horizontal < 0 && transform.position.x > -2.57 && gameController.started)
{
transform.Translate(-Vector3.right * speed * Time.deltaTime, Space.Self);
}
}
But this only works for the resolution that I am using. How can I make the player stop at the edge of the screen for every screen resolution?
orb
April 1, 2018, 12:18pm
2
Camera.WorldToScreenPoint might be a good start. Screen coordinates start in the bottom left with (0,0), and go to the top right where it’s (width,height). Just grab the resolution at start and calculate edges off that.
orb:
Camera.WorldToScreenPoint might be a good start. Screen coordinates start in the bottom left with (0,0), and go to the top right where it’s (width,height). Just grab the resolution at start and calculate edges off that.
I tried this, on the left side it worked, right side every time it was wrong when I changed the resolution.
orb
April 1, 2018, 1:10pm
4
OK, just work directly off Screen.width/Screen.height (don’t cache), as there’s no screen resolution change event to watch for.
I fixed it with this:
float horizontal = Input.GetAxisRaw("Horizontal");
float mapWidth = Camera.main.aspect * Camera.main.orthographicSize - 0.25f;
Vector2 newPosition = rb2d.position + Vector2.right * horizontal * speed * Time.fixedDeltaTime;
newPosition.x = Mathf.Clamp(newPosition.x, -mapWidth, mapWidth);
rb2d.MovePosition(rb2d.position + Vector2.right * horizontal);
rb2d.MovePosition(newPosition);
But how in godsname am I gonna make my entire game responsive? I have an endless runner, bricks spawn and fall down, but the size of the bricks have to go with the resolution for example.
orb
April 1, 2018, 1:31pm
6
Search the forums for “endless runner” and see what comes up. People make those sort of games all the time