How does one handle different screen resolutions (Based on Unity 102 Spaceship) ?

In your example game made in the Unity 102 class, he locked the resolution to the web player.

When I build the example for PC and increase or decrease the resolution, the player can move off the screen. How can you make the left and right boundaries be at what the camera is seeing? AKA How can the boundaries change with resolution changes?

This seems to happen to all my test games. I start a desktop game and make the game with boundaries on the screen, and the game view is on Free Aspect. When I export it to build, all the sides of the arena are cut off and the player can go off screen to them.

dberroa: Sorry - I’ve not had the time to put together a good example. I’ll try to get to it asap.

The first way I’d do this is to grab the viewport of the camera and find the world space coordinates for that viewport at the game plane.

Camera:

ViewportToWorldPoint

Then, once I had the “bounds” of the game as defined by what the camera can see, I would need to find the size of the Player Ship. To do this, I would use Renderer.bounds, as this returns a world space value, whereas Mesh.bounds returns a local space value.

Renderer-bounds

Mesh-bounds

Then I could calculate my x/z Min/Max clamp values.

Specifically:

Create a public reference to the camera being used (or use Camera.Main* instead in the code below):

public Camera myCamera;

*Read the Camera Component docs for more information of “Camera.Main”

Set the x/z Min/Max clamp values to

	private float xMin;
	private float xMax;
	private float zMin;
	private float zMax;

Then, in Start(), find the bounds of what the camera can see via ViewPortToWorldPoint, find the HALF of the size of the ship with renderer.bounds.extents, and then set the x/z Min/Max clamp values

	void Start () {
		// Find the topRight and lowerLeft of the camera Viewport in World Space
		Vector3 topRight = myCamera.ViewportToWorldPoint (new Vector3 (1.0f, 0.6f, myCamera.transform.position.y));
		Vector3 lowerLeft = myCamera.ViewportToWorldPoint (new Vector3 (0.0f, 0.0f, myCamera.transform.position.y));
		
		// Find the bounds of the ship
		float shipWidth = renderer.bounds.extents.x;
		float shipLength = renderer.bounds.extents.z;
		
		
		// Set the bounds for the clamp position by code
		xMin = lowerLeft.x + shipWidth;
		xMax = topRight.x - shipWidth;
		zMin = lowerLeft.z + shipLength;
		zMax = topRight.z - shipLength; // ShipLength isn't 100% necessary as this is an aesthetic choice within the play field.
	}

NOTE: The “0.6f” value on line 3 in the “Start” function: Vector3 topRight = camera.ViewportToWorldPoint (new Vector3 (1.0f, 0.6f, camera.transform.position.z)); This sets the upper boundary within the game for the player ship. You could make a new public variable, and tweak this in the inspector:

	public float playerLimit;

and change the line in “Start” to use this variable:

Vector3 topRight = camera.ViewportToWorldPoint (new Vector3 (1.0f, playerLimit, camera.transform.position.z));

For these two lines:

Why is the Z value the y position of the camera?

If you look at the documentation page for ViewportToWorldPoint:

You won’t know where the world point is unless you have some reference point - in this case the camera. The camera is important over some arbitrary reference point if you think about the camera as a frustum (or tipless pyramid) so the viewport to world point varies not only in distance from the camera but in field of view. The shape of the frustum is defined by the field of view, so you need to know where the camera is, what the field of view is, and how far away from the camera the point is, to determine where in the world it will be.

In our case, our game plays on y=0, so you can use the camera.transform.position.y for this value.