# 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?
– House
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 Aug 7 '17 at 23:23
• I assumed classic arcade moving toward the top of the screen. Does the camera move?
– House
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 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
– House
Aug 7 '17 at 23:27

You can restrict the player position to the viewport like this:

void Update() {
Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
pos.x = Mathf.Clamp01(pos.x);
transform.position = Camera.main.ViewportToWorldPoint(pos);
}


That converts the position of the player (assuming or any object this script is attached to) into viewport coordinates, which range from 0 to 1. Then clamps the position between 0 and 1. Finally converting back to world space. So, if the object is outside of the screen, its position will convert to something less than 0 or greater than 1, which this script will correct. Otherwise, it'll leave the position where it is.

• thanks again. This has helped me understand another little piece of Unity and coding in general. Aug 9 '17 at 16:06

A slightly different approach to the one given by Michael, which I am using on my project, if you are using crossplatform input axis and convert it to vector3 for movement;

     public float h;                            //input axix x
public float v;                            //input axis y

private void FixedUpdate()
{
h = CrossPlatformInputManager.GetAxis(axisX);
v = CrossPlatformInputManager.GetAxis(axisY);

Vector3 viewPos = cam.WorldToViewportPoint(transform.position);

if (viewPos.y < 0.02f)
{
v = 1;
}
else if (viewPos.y > 0.99f)
{
v= -1;
}

if (viewPos.x < 0.01f)
{
h = 1;
}
else if (viewPos.x > 0.98f)
{
h = -1;
}

m_Move = v * Vector3.forward + h * Vector3.right;

}