Unity — Fight or Flight

Alain Schoovers
4 min readMar 23, 2021

One of the most basic responses to danger is Fight or Flight. So let's work on this first.

We gave our little block the power to move all around its little environment! But before we unleash it into this scary new world we need to give it some basic response.

Flight

Of course the safest way to respond is simply to run away! our little guy can already walk but now let's make it run.

Let's make it so that when the Left shift key is pressed our speed triples.

First we want to triple the speed we can do this by multiplying our movement(transform.Translate) code we used in the previous section by adding * 3 to the calculation like so.

Now we need our script to know when to use this awesome new power! We do that by nesting the code into an if-else statement. The first step is to define the condition is we want to use, in this case we are using LeftShift.

In order to know if the key is kept pressed we use Input.GetKey with the appropriate keycode.

finally, we need the speed to revert to its original when the key is released so our little guy can take a breather, for this we use the same code but without the * 3 multiplication nestled within the else part of the code like below.

The if statement runs as long as the condition within the ( ) is met, if not it will fall back to whatever is in the else portion of the statement.

Now save your script and watch how our little block runs happily across the screen.

Round one Fight!

Sometimes in life there is no running away and you will have to stand your ground. Let's give our little friend some way to fend of potential danger and what better way than shooting cylinders of death!

Let us make it so that when you press space we can shoot some cylinders across the screen.

This time we start with an if statement like above. This time we want it to listen for the spacebar to be pressed once, we use Input.GetKeyDown this lot triggers once when the defined key is pressed so it's perfect for what we need. Now all we have to do is add the Keycode.Space and we are halfway there.

Now we need to create a bullet whenever the key is pressed. in Unity we call this Instantiate.

Instantiate creates whatever prefab you define with set position and rotation data we use Instantiate(PREFAB, POSITION, ROTATION);

First we add a new variable of the type GameObject with the name bullet so we can set that in the inspector after we instantiate that bullet on our current position(transform.position) and a default rotation(Quaternion.identity)

If we now create a capsule and make it into a Prefab by dragging the capsule into the Project folder and from there into the script component of our cube we can now use that as our projectile.

If you save and press play you can now see that whenever we press the space key we see our capsule appear on screen!

Our little friend is now way more prepared for the world that lies ahead.

The only problem now is the bullets don't move. but don't worry we will sort that out next time!

Hope to see you there, thank you for reading and as always happy coding!

--

--