# How can I make my player to stay inside the screen? C#

Hi guys…

Can anyone help me out on this topic, please? My player is at the bottom of the screen and goes right and left only… so I would need to limit its x to be always inside the screen…

It is a 2d game, but, the scrolling sky is set in a plane (2 of them, actually)

Instead of limiting the speed of you character, why not make your camera catch up to your character?

The scene is like “1942” old NES game… you have your background, the player at the bottom, moving only left and right, and enemies coming from the top… so you always see your player…unless you go off screen

So what I need to do is to make some limits, on the bottom right and left corner of the screen, regardless of the size, for the player to stay in the screen

Maybe adding GameObjects with Colliders associated, so that you restrict your player from going off-screen? That is a solution I have always adopted. The only problem is that at high velocities, your plane will go through the colliders since Unity3D Physics mechanism is unable to resolve that situation. And at lower velocities, your plane will most like shake.

Those are the reasons why I never liked this solution…

Cheers

EDIT: Maybe another solution could be making a transition, so that if you go through the left side of your screen, your plane would automatically appear at the right side. This is a solution often used in commercial 2D games and if you do it correctly the result should be more than just acceptable.

Use Mathf.Clamp. It’s the easiest and the best solution and it’s pretty much for clamping something from a value to another value.

1 Like

I didn’t give it a try but appears to be the best solution in many cases. Thanks!

This needs some edits if you are using a perspective camera, or a camera that isn’t the main camera, or the x axis isn’t your horizontal axis.

``````void ClampTransformHorizontallyToCamera(Transform playerTransform)
{
float leftBound = Camera.main.ViewportToWorldPoint(Vector3.zero).x;
float rightBound = Camera.main.ViewportToWorldPoint(Vector3.right).x;
Vector3 newPosition = playerTransform.position;
newPosition.x = Mathf.Clamp(newPosition.x, leftBound, rightBound);
playerTransform.position = newPosition;
}
``````
1 Like

Aren’t world coordinates in Screen Space?

I might’ve misunderstood the question. This is what I understand world coordinates and screen space to be:

• World Coordinates: The (x, y, z) position of a transform.
• Screen Space: The pixel index (x, y) where (0, 0) is the lowest left pixel on the screen.

These will only match up under specific circumstances.

• The camera is pointed in the positive z direction.
• The camera is orthographic and has orthographic size exactly 0.5f *Screen.height.
• The camera’s position is (0.5f * Screen.width, 0.5f * Screen.height) plus a half-unit offset. I forgot which way the half-unit offset needs to be.

If the player has a high resolution screen, this means that the camera sees more world, and thus everything is smaller. This is usually not desired, though I have set up some GUI/HUDs like this to be pixel-perfect.