Nerd For Tech
Published in

Nerd For Tech

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.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Linked List explained

linked list explained banner nickang

LeetCode 1.Two Sum

Solving a programming problem like a programmer

Performance benefits of using Service Worker in Angular app

50 Javascript Best Practice Rules to Write Better Code Framework

Vue Image Right Click Link and Left Click Use onClick Event

Coding

Creating A Virtual Nostalgia Simulator

Load data from Stripe to BigQuery

How to use React context to share state between several components without passing props

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniele Quero, PhD

Daniele Quero, PhD

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams

More from Medium

2D Light Flicker in Unity

Journal 146 — Unity Project Development, 2.5D Shooter

Using the Unity Profiler

Making Piano Tiles with Unity