Project 1: Space Shooter
Introduction to simple player movement in Unity
Also how to add a Player object
Alright finally to the actual development part, but before I can jump to the fascinating topic of moving my player I have to first create one. Since I’m prototyping my game it doesn’t matter what the actual player looks like, so I will pick the first 3D object that Unity offers, namely a cube. This can be done easily with clicking with the right mouse button on the Hierarchy view and selecting a cube from the 3D objects and naming it Player.
The next step is to make a script for my player. To add a new script for my player all I have to do is to right click Scripts folder in the Projects view and select Create and click on C# Script from the menu. Be sure to name it immediately, because that name will also be the class name. It can be changed later, but it will be a bit more involved process, so it’s better to name it correctly after creation.
Now all I have to do is to attach the script to my player object by dragging the script on top of the player object in the Hierarchy view and releasing the mouse button. I can verify that my player object has a script by selecting the object and looking at its properties in the Inspector view.
Adding movement for the Player object
Then it is time to add some simple movement for my Player object and see how it works. First I double click on the Player script to open it in the Visual Studio, and then add the movement. First I try it out with just the simplest way. Even though the player zoomed out of the view almost instantly, the code still worked.
The transform part tells Unity that I want to access the Player object’s transform properties. The Translate on the other hand causes my Player object to move. In this case I want to move it to the right with the Vector3.right variable, which moves the Player object to the positive side of the x-axis by increments of 1. Why did it move so fast then? That’s because I put my code into the Update method of the Player class, which updates the Game view once per frame. The Game view itself updates as many times per second as it can render the image.
To update the movement once per second Unity offers a class called Time and it has a method called deltaTime. Applying this to the code makes my Player slowly crawl to the right. To add more speed, all I have to do is to multiply what I already have by some number, I choose you number five.
So my Player object moves to the right with the speed that I want, but what about other directions or stopping for that matter. For this I can use Unity’s Input class’s method GetAxis to not only change the directions and stop but to get user input to do that. It incrementally changes between -1…1 and returns to 0 once the key is released. The alternative method that Unity offers is called GetAxisRaw which has only three values -1, 0 and 1.
Since I want to move my Player object along the x- and y-axis I have to do two Translations, for the y-axis I use Vector3.up as my Translation direction. For the movement on the x-axis I call the method with the “Horizontal” argument, and for the y-axis with the argument “Vertical”. To actually move my Player object I can use either the usual WASD or the arrow keys on my keyboard.
Now I have simple movement implemented for my Player object.