how to make the player stay within screen in a simple 2d game. C#

So i just got into unity, i moved over from LibGdx(java) and everything is really new to me. I’m just trying to make a square move around a screen with a simple script. But i can’t seem to figure out how to make it stay within the screen limits(pretty sure it’s simple and i’m just stupid) i’m just testing it out by only using it on the right side on the screen, but shouldn’t this work:

public class SquareScript : MonoBehaviour {

private float moveSpeed = 10f;


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	Vector3 playerPos = transform.position;
	
   if(Input.GetKey("left"))
		transform.position -= Vector3.right * moveSpeed * Time.deltaTime;

   if(Input.GetKey("right"))
		transform.position += Vector3.right * moveSpeed * Time.deltaTime;
	
   if(Input.GetKey("up"))
		transform.position += Vector3.up * moveSpeed * Time.deltaTime;	
	
   if(Input.GetKey("down"))
		transform.position -= Vector3.up * moveSpeed * Time.deltaTime;
	
   if(playerPos.x > Screen.width){
		playerPos.x = Screen.width;
		transform.position = playerPos;	

	}
}	

}

it doesn’t seem to work at all, also. If anybody know any decent turtorials on simple 2d scripting in unity, that would be awesome. Any help is GREATLY appreciated!

Thanks a lot

If you are working in an orthogonal view or top-down perspective, you can simplify the problem by making your Ortho equal to your screen space dimensions. In other words, the issue you are having here is that the playerPos is in world space, but if you’re camera is not configured so that world space X value at the right of the screen is equal to the Width (in pixels) of your viewport, then you can’t use the playerPos with the Screen values.

Since the Editor doesn’t allow you to set the left/right/top/bottom of your Orthographic projection to the specific pixel resolution you intend to run at, and I don’t know what the Editor Camera’s Size property actually reflects, the approach I would use is call:

Camera.ScreenToWorldPoint()

This will transform a screen point into the world point. Pass the right/left/top/bottom screen points in to find the correlating world point. Then compare that world point to the value of playerPos in your code above.

You will also want to add in the half-size of the box so that the entire box remains on screen. The following is an example to test the right side of the screen. It assumes X axis is positive to the right and that you are doing this in a non-static function as part of a MonoBehaviour-based script. Also note this should be optimized to determine the box size and world positions in the Start() method if possible:

Vector3 wrld = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, 0.0f));
float half_sz = gameObject.renderer.bounds.size.x/2;
if(playerPos.x > (wrld.x - half_sz))

I tried to make sure this is valid code, but if you have any errors here, let me know and I’ll correct.

Solving your problem and answering your question about a 2D tutorial, watch this series from quill18creates about a 2D brick game, I’m sure your problem is addressed there - Really great tuts he got, very good programmer, very friendly to new-comers and explains things in good depth.

I created an empty game object attached a box collider to where I didn’t want my player to go. I found that to be extremely easy and straight forward

@nickkorage collider doesnt work when object hit collider it moves.

I made the edges on 4 sides by taking a quad and add a Rigidbody2D and box collider2D to it. But still in my game, ball goes beyond this edges when it cross the speed I mentioned it. Any suggestions for it?