Adventures in GameDev with GameDevHQ! Day 8 — Simple Player Movement in Unity
And now the moment of truth! It’s time to take our first steps into Unity game development!We’re going to put something on the screen and we’re going to move it around! Let’s go!
What many beginning devs do is they want to build the next Massively lore deep Final Fantasy 27, Skyrim, or Super Tentacle Bikini Battle FunKilltime, but then give up as they are overwhelmed by the amount of work involved with game design and mechanics, character, world, sound, UI, and level design, coding, etc. That’s a lot of work! So let’s take baby steps…Our goal for today is:
Move a cube.
That’s it. Our cube will represent our spaceship that will fly around the screen so let’s prepare a few things. Select the main camera and in the inspector go to clear flags or just the top option where it says ‘Skybox’. Skybox is basically a cube that surrounds the world giving the illusion of a sky, but since we’re making a 2D shooter, all we need is a black background so click on skybox, select solid color, and then the color picker and select black. Easy Peasy!
Then we’ll put a cube on the screen. This will be our temporary representation of our awesome spaceship. We’ll also call him Player.
Now a cube by itself won’t do anything, we need to give it instructions on what to do, so like placing the ward into a golem, we’ll create a Player script and attach it onto the Player cube as a component.
Normally, you’de right click in your scripts directory and select new script. The window went outside the recording area so I just did it inside of the inspection window while the player cube is selected. Always remember there’s multiple ways to do something in Unity!
So how are we going to move our player cube? Let’s take a look at the inspector. The first thing in the inspector is the Transform component. It holds all the information for position, rotation and scale. So let’s say we modify the x and change it from 0 to another number, let’s see what happens.
Awesome! So we know transform affects the placement and movement of the cube! We can check out unity’s documentation to see how we can use this for our movement purposes!
Doing a google search takes us to this page:
There at the bottom of public methods says Translate. Moves the transform in the direction and distance of the transition. Sounds like what we need! Let’s check out the page!
There’s an entire page of how to use the function but this is all we need!
Now another explanation, what’s a Vector3? Simply put a Vector3 is how unity knows the x,y, and z location of a transform at any time. x being horizontal, y being vertical, and z being forward and backward.
That’s why when we were changing x, the cube was moving along the horizontal axis!
So let’s experiment. Let’s use one of Vector3’s built in components Vector3.right to move the cube.
Let me quickly fill you in on Unity scripting: When you open a script for the first time, there will be two functions: Start(), and Update(). Start is what gets run for the first time when the object is born into the scene. Another word for that is Instantiated, but we’ll get into that later! Next is Update(). Update gets run approximately 60 times a second. or 60 frames per second. So anything that gets put into update, will be executed…you guessed it…60 times a second. Now you may be guessing at this point, wait…if we’re going to be moving right 60 times in one second, won’t that be a lot of moving? Well, let’s find out!
Whoa! Aside from a bit of a hiccup, that cube took off! Why?
Well, Vector3.right is essentially the same as Vector3(1,0,0).
Reminder that the 3 numbers are for x, y, and z, so that means that every 60 seconds, we’re moving 1 unit to the right, which is the equivalent of 1 meter, 60 times a second. so that’s 60 meters a second! Hot dang! We’ve got ourselves a mazarati of a game engine! That’s too fast! What can we do to slow it down?
So what is it? There’s a complicated explanation about the time between the last frame and the current one, but in plain english, it’s 1 second. so whatever you multiply with it gets translated into a second of movement, so if we say we want to move one unit, instead of moving 1 unit for 60 frames, we’ll now move 1 unit for a second. So let’s give it a shot!
That’s more like it! Now it takes a full second for the cube to move one unit! But what if we want it to go faster? We just multiply it by a new number, and that number will be the speed of the cube. For example, let’s say we want the cube to move 3x fast…
Now let’s hit play:
Quite a noticable difference!
To explain, Vector3(1,0,0) is now being multiplied by 3 so now its Vector3(3,0,0) being called 60 times a second, but thanks to Time.deltaTime, we only move 3 units in 1 second!
If we wanted to move left, we could also change the speed to -3.
Why did this happen even though we’re using Vector3.right?
remember again, Vector3 right is Vector3(1,0,0) and if we multiply it by -3 it turns into Vector3(-3,0,0) which means it moves -3 spaces every frame, or to the left! multiply that by time.deltaTime and you move 3 units per second to the left! Pretty awesome, eh?
Ok, another quick detour before we get into player input, let’s get into variables.
variables are basically boxes that you put information into and they can be changed and vary, hence…variables!
there’s 4 general variable types.
int — integers, whole numbers. Ex: 3, 9000, 5, etc.
float — numbers that have a decimal in them. 9.3f, 7.7f, 1.20583f, etc. you always use an f with the number to denote a float.
bool — true or false, default false. True or False.
string — a string of characters strung together! ex: “Steve”, “Potato”, “This entire sentence is a string”, “Health: “, “Lives: “, etc.
variables can be public or private. a public variable can be accessed from anywhere, and a private variable can only be accessed from inside the script it originated from. It’s generally good practice if you’re going to be using private variables, to put an underscore in the name.
You create a variable by first declaring whether it’s public or private, then give it a type, then a name, and then optionally you can assign it an initial value.
private int moneyIhave = 0.00
public float health = 98.821
The reason I bring variables up is we’re going to stick our speed in a public variable. Public variables will appear in the editor and make it easy to modify them in real time.
Now we have a variable for speed and the cube will move just like it has before but as you can see, because the player speed is public, I can change it in real time in the editor! handy!
You can also make private variables available in the unity editor by placing [SerializeField] a line above it. Sometimes hand if you don’t want teammates getting lost in your code looking for a certain variable just because it was private.
If we look into the project settings, we’ll see Unity has an Input Manager. Unity already has a set of built in controller properties so if you want to control the game by keyboard, joystick, touchscreen, whatever, Unity has it and you only need to access all of them via simple axis names like Horizontal and Vertical. Whodathunkit? So we want to get our player to move side to side. How do we get our horizontal input?
Short answer: Input.
Input is how we get input from the player. and if we want to use these axises that are already making things easy for us, we use Input.GetAxis. Now if we want our player to move from side to side using the arrow or A and D keys, that’s a built in function of “Horizontal” axis. Specifically:
Input.GetAxis(“Horizontal”). Let’s implement it into our current movement script!
Now let’s test it!
HUZZAH! IT WORKS!
Pressing left and right arrow keys or A and D now moves our player cube!
Can you guess how we can move it up and down? If you guessed Vector.up and grabbing Input.GetAxis(“Vertical”), you’re right! So let’s do it!
Looks good! Let’s try it…
NEAT! Congratulations! Now it’s time to publish and get rich!
Well, maybe not yet, but we got a player moving around and we understand how and why it works so that’s not too shabby!