Unity — Taking control

Alain Schoovers
4 min readMar 22, 2021

--

Last time we talked about the concept of movement. It made the game feel more alive but it feels like something is still missing… Its control! today we will dive deeper into how to achieve this using Unity.

Control

So first off what is control? One definition is “determine the behavior or supervise the running of.” Simply put it is defining and setting the behavior you would want our objects to adhere to, this can include things like speed, direction, aggression boundaries and any other types of behavior you can think of depending on the object you are creating.

Speed

Speed might sound like a complex concept but when you break it down it's nothing more than a value you multiply your movement(Translate) with. Just like we did with Time.deltaTime we can also introduce speed into the mix.

Speed is nothing more than a number you can use directly or even store in a variable so you can easily adjust it later.

By adding 3.5f or the _speed variable to the code we now have control over the speed, in this case 3,5 times as fast as before.

Please note that we use concepts like “variables”, “Input manager” and “If statements” don’t worry if you are unfamiliar with these since we will talk about these in great detail in a later post.

Script movement vs Player input

When building your game there are a lot of things that might be moving, some of them have simple movement like a powerup dropping down on your screen or an enemy moving from side to side.

Simple movement like this is mostly done through a script that runs automatically and will react to things in a way you pre-defined within its scripts like collisions, detections physics and any other rules you set within your script (no worries we will discuss these topics in a later post) you can see this as sending someone off with instructions to make you a cup of coffee and simply waiting until they (hopefully)bring you a nice and hot brew.

Photo by Mike Kenneally on Unsplash

It is great objects can react to each other or on their own and even take instructions but what if we want more control and adjust the behavior in realtime? In come player controls.

Player controls are nothing more than creating some behavior that is linked to some sort of user input. For example “Shoot when space is pressed” or “move right when D is pressed”.

Let's take a look at how we set these up, first of we put all of our code in the “void Update” this will keep running and listening until it detects input from the player.

Now we have to “sense” what input is given by the user so we can crate behavior for this. If we are defining player movement it is good practice to use Unity's input manager we do this in the following way.

Input manager is a pre-defined way of detecting commonly used player controls used in games.

First we have to create a variable that stores this input in this case “float verticalInput” when we have this we set it to be equal to the user's input with “Input.GetAxis(“Vertical”)” this will give a value of 0 when nothing is pressed -1 if down is pressed and 1 if up is pressed (or W and S).

Next all we have to do is plug this into the code that is now controlling movement and behold we now have control over the vertical movement of our player!

Now we do the same for the horizontal input and we now have almost full control over our object, awesome right?

Now you have some basic control over the movement but that's not all you can do, next time we will take a look at other types of inputs you can use, and link these up with some more awesome behavior you can add to your objects like Firing and Boosting your speed.

Hope to see you there but for now, thank you for reading and happy coding!

--

--