Search Unity

My player runs ouf of screen

Discussion in 'Editor & General Support' started by csviktor, Jan 24, 2021.

  1. csviktor

    csviktor

    Joined:
    Jan 2, 2021
    Posts:
    5
    I'm moving my 2D player left and right with this:

    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

    The problem is that the player runs ouf of screen and I don't have an idea how to stop him. I tried to use the transform.position.x to ask the actual position and then give him a new Vector2(0, rb.velocity.y) to stop. But once it stopped I was not able to run him again. Do you have any hint what should I do?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    There isn't really one single way to do this and it kinda depends on your game's design.

    You could put colliders around the edges to keep him onscreen, or else use the camera to find the corners of your viewed screen and either contain the player yourself with if checks, or many other ways.

    To get a feel for what's involved, try out some simple tutorials where the player moves around a non-scrolling screen.