Unity Game Dev : create a Player Bounds with “if statement”! (Part I)

Matteo Lo Piccolo
3 min readMay 5, 2021

Is time to make some “feature” in our game!
We want create a space shooter, and one of mechanics of the game is block our “Player” from the middle of the screen and down and make it wrap from left to right and vice versa.

How can we do?
In both cases we have to check the position, so once again we have to work with Vector3.
But we must also introduce another extremely important tool in programming: the IF STATEMENT.

What is the IF STATEMENT?
As the name suggests, it allows us to make DECISIONS within the logic of the code.

I create “IfStatement” script in our project and attached to Main Camera, just to make some example.

This is our script

The logic is this : IF I HAVE SOME CONDITION, RUN THE CODE
And the syntax is this

We can simply type if and press TAB two times to autocomplete

“True” is the condition

Now we create a simple problem to solve :
I have 16 years? I can drive.
I have less than 16? I can’t.
Really easy

So, first we create the variable for age

Now we need a logic with condition to return us this :
If we have more than 16 years write in the console “You can drive!”
but if we don’t have it, write in the console “You are too young!”;

This is a simple logic, right?

To help ourself to THINK at the logic, a very good way is create a
PSEUDO CODE.
We simple create a comment and write down what we have to do

We write what we need to do, and then we turn it into code. Very simple but incredibly effective!

The code we will extract from this PseudoCode will be:

We stop one second here to explain what happen :

> followed by = means MAJOR OR EQUAL TO
So the condition inside if statement is
“If myAge is major or equal to 16, run the Debug.log()”

If we press Play

My Age is 22, and because IT IS MAJOR of 16, the code inside run. Good!
If the condition is true, the code inside run!

IMPORTANT THING TO DO : always create a variables. ALWAYS.
I write directly the number 16, the best way is create a variable to reference it

and use it inside condition

Much better, right? Better to read, better if you need to change something. Now we have a small project and a small script, but if we have many many lines we cannot scroll down to look for the single data, we always take a reference.

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!