Simple Player Movement in Unity
Day 1 — Aspiring Game Creator
2D Game Dev: Galaxy Shooter
Unity Hub, Unity, Git version control & GitHub offsite backups are in place. Very cool! So, Game Creator … where to begin? Some of my favorite games are: Fallout 3, Bioshock, Overlord, Diablo, The Division, The Witcher, South Park, Red Dead Redemption … I have ideas jotted down somewhere for many similar adventure type RPG — shooters!
Where to start …
Well, every game I love has one major thing in common: a PLAYER!
So let’s start there.
Here’s my Prototype Spaceship:
Each and every game object in Unity has a common component: TRANSFORM.
Each TRANSFORM is made up of 3 parts: POSITION, ROTATION & SCALE. Manipulating the transform’s position is how the player will move from side to side, up and down and diagonally.
BEGINNER’S TIP: Every component in the INSPECTOR has a reference, simply click the ? These references may not give exactly what is needed to solve an issue but it’s always a solid starting point.
Below, the TRANSFORM docs shows a method available to MOVE the transform: TRANSLATE.
The TRANSLATE method will move the transform, i.e. a game object, in the direction of translate. The translate in this case is a new Vector3. A Vector3 is a 3D position or direction. In case of moving it is a direction.
In order to move the game object at a consistent speed across all devices Unity has Time.deltaTime.
Multiplying the direction by Time.deltaTime will ensure that the game object is moving one meter per second.
Introducing a speed variable and setting it to 5 will then move the game object at 5 meters per second.
User Input
Moving the player to the right at 5 meters per second is a cool start, but games need user input.
In order to capture the user’s input and control the player through script, Unity has its INPUT MANAGER. Capturing input for: keyboard, mouse or controller is pretty easy.
Day 1 complete.
Day 2 will be about variables and locking down player movement with boundaries, but with the option to wrap from right to left and left to right.