If there’s one thing that we all can agree upon, that’s the fact that every game has an object that we as players control, be it an FPS/TPS where we control an avatar or a Puzzle game like Tetris or Chess where one moves the tiles or game pieces. In this scenario I have chosen a cube as my partner as I develop this prototype.
Setting up the Player !
Let’s start by getting the player set up. We can do that by doing the following (To understand the basic terminology and an overview of the editor you can click here),
An alternate way to do this is, select the “+” button found on the top left corner of your “Hierarchy” panel and this should present you with a list of options one being “3D Object” select it and select whatever object you wish to make as your protagonist(“cube” in my case).
In case you were like me and you don’t see your created object in your game view, this could be because your object got created behind the “Main Camera” in that case just select your newly created object(Player) and reset it’s transform by doing the following.
Styling the scene!
Before we move forward with player movement, let’s add some style in your game. Let’s start with adding colour.
Make sure to drag your “Player_Mat” material onto your “Player” game object in-order to get your “Player” a colour as done above.
It is important that your game looks the same on all platforms irrespective of the size of the screen. To do so you can set an aspect ratio for your game. I’m going to go with “16:9”.
If you wish to you can create your own custom aspect ratio by clicking on the “+” button.
Let’s start out by creating a script.
Upon completion, open the script and let's get your player moving.
The first thing needed to get our player moving is taking user inputs. But before progressing further you first need to familiarise yourselves with Unity’s inbuilt Input Manager system.
Here we observe that for horizontal movement we use the “horizontal” axis and for the vertical movement we use the “vertical” axis.
Here “Input.GetAxis” returns a value ranging from “-1” to “1” when nothing is pressed it returns “0” but let’s say we press “d” or the “right arrow key” this returns a value greater than “0” and if the button is held down for a few seconds it returns “1”. So basically, it returns gradually increasing or decreasing values depending on the direction, where the upper limits are “1” and “-1” respectively.
Every game object has a “Transform” component attached to it which contains position, rotation and scale of an object. Manipulating the position can get your player moving. In order to influence it to move in a direction and distance we wish for it to move we use the following.
It’s observed that “Translate()” requires a 3 dimensional Vector determining the position of where your “Player” would move. i.e if we were to do the following
This would imply that our player would move to the position (1,0,0) in our coordinate plane, and if we want our player to move right continuously we just have to put the above in and “Update” Function which basically runs once every frame, so if your game runs at 60 frames per second then the “Update” function will be running 60 times. But this would mean if our game were to run at 120 Frames a second then we would move faster, so this would imply that the more powerful the system the faster we move. In order to avoid this and make movement frame rate independent we use,
So, putting all of it together we can say that in order to move according to user input we can do the following,
This is how I got my cube to move.