# How to restrict the players movement win relation to screen bounds in Unity? (Without colliders)

I am really enjoying making the switch to Unity. But here I have found one of those rare moments where it appears to make things more difficult (at least it is for me as I am crap at understanding the actual manual)

Basically my 'player' is a space ship. It starts life at 0,0,0 world coordinates and I have been doing all the movement based off of that. (ie. Add force X to move left or right, and there is a constant Z speed which is done via rb.transform.Translate().)

I want to restrict the movement on the X Axis, so in Libgdx id use something like:

if (player.position.x <= 0){ player.position.x = 0;}
if (player.position.x >= viewport.getX()){ player.position.x = viewport.getX();}


The thing is that wont work for me in Unity for several reasons (1. I dont know much about the viewport as I didnt create it. 2. the player.position is being affected by the rigidbody forces. AND i am working in world coordinates with the players position).

As I said, the player is constantly moving forward on Z axis, so making colliders doesnt seem like the right choice for me.

I'm pretty sure Unity devs would of put this in Unity v0.001 but I just cannot for the life of me find how to actually do it. (Any info I can find seems from like 2011 and also is pretty badly written code making this simple matter takes dozens of lines of code and some methods - I'm hoping there is a Unity method already made, like player.transform.ScreenPosition or something but yeah I cannot find it!?

Any help would be massively appreciated as always.

• Based on your description, I'm guessing top down 2d? – MichaelHouse Aug 7 '17 at 23:21
• ahh, sorry! Its top-down 3d . Hence the Z axis bit :) But the camera only stays at one height and purely follows the player forward. But the player can dip and rise to some degree and also go left and right – Big T Larrity Aug 7 '17 at 23:23
• I assumed classic arcade moving toward the top of the screen. Does the camera move? – MichaelHouse Aug 7 '17 at 23:25
• @Byte56 soz it is a terribblee description i gave. Its like the old style arcade topdown shooter, but using 3d models. The camera only follows the player along Z axis. The background is two planes, that roll over when they have been passed. But I cant restrict the right-left movement properly – Big T Larrity Aug 7 '17 at 23:26
• OK, so can't you set the X bounds based off the camera X position? Like no more than 8 units from the camera's X position at any time? Camera.main.transform.x is the "center" of your view – MichaelHouse Aug 7 '17 at 23:27

void Update() {