Bonus: You Shall Not Pass!! — Player Boundaries
Objective: Prevent the Player from reaching outside the screen
This a little more advanced, since I plan to explore some topic better in future tutorials, so consider this as a bonus and maybe come back here later for a better understanding.
The issue is simple: now that the player can move its first steps (so cute!) it can breach the screen side… if we don’t stop it!
There are two ways, one easier but messier involving a simple check of player position, compared with some empirical value we can get from the inspector and consider as screen boundaries. The other one, which I prefer, is more “programmatical”, we’re going to get real time screen boundaries that may even change during the game because related to game camera configuration. Of course it is more complex, but not impossible.
Ok, I’ll put it down as pseudo code: we have to stop the player at screen boundaries; but to do so we have to get these boundaries; but to do so we have to define them, for example using the size of what the camera sees.
First let’s create a new C# script and name it CameraBounds.
Let’s define some variables in the usual fashion: reduce constructor calls if it has to be done too often.
Ok, we need to dive deeper: _cameraVisual will store x and y size of the screen, as seen by the camera, initialized to some randomly evil values. _camera is a handle on Camera component and then I defined a _cameraVisual as a property to get public access on it.
Here we assigned to _camera the Camera component, we need it to grab some values. It’s n start so it’s done just one time at the beginning.
A nice part: a private method UpdateCameraVisual(), here we call Screen class properties, width and height, in order to calculate aspect ratio. Maybe there is a simpler way… but it never came to me!
Now the real stuff: y component of the visual is obtained by the orthographicSize property of the camera, representing just half the vertical size, but that’s exactly what we need. The horizontal component is derived multiplying y by the aspect ratio.
We finish this class with a call of the method in the Update().
Back to player class, let’s add two new variables: a handle on CameraBounds script (_cameraBounds) and a vector3 storing player position (yeah, 3 even in 2D).
In Start() we can link _cameraBounds to the component, safely, by checking if the camera object we are looking for is present (not null). To search the camera object I used the method GameObject.FindGameObjectWithTag(“MainCamera”), which is quite self-explanatory. The tag is indicated in the inspector view.
Debug.LogError() will show the specified string, marked as error, in console.
Now the core of the issue. Let’s write a new private method CheckBounds(), here we will put our logic. Basically, we are going to set the vector3 values for the position using a powerful method: Mathf.Clamp(). This method asks for a float value to check, a float value representing the minimum and another one for the maximum. It returns a float value which is equal to the originally passed one, if between minimum and maximum, or minimum (if lower) or the maximum (if greater). Splendid!
Now we call this method in Update() and we’re good to go.
Here’s the result:
Just one thing before closing: I chose this method because in this way, if I modify the camera, the boundaries will update consequently. Namely it is done by changing the Size value in Camera Component from the inspector.