I’m starting off by making a 2D game that will be a space shooter called Galaxy Shooter. A simple game where the player will have to take out as many enemies before you’re defeated.
Objective: create a prototype representing the player’s movement.
To start, create the player using a 3d cube, and create a material to change the color of the player to a color that can be seen over a dark background. Use the “+” sign in the Hierarchy for the cube, and use the “+” sign in Project for the material. You can also right-click in the respective areas to create each asset as well.
Now we want to change the background to black to give it more of an outer space look for the moment. Click on Main Camera, then under Camera click on the background color box, and change color to black.
Inside the Game tab, set the aspect ratio to 16:9 because it’s high definition, and deploys to different platforms like TV, monitors, android or IOS devices.
Next, we will create a Player script to control the player’s movement. But, before we do we should set up the default editor that will be used every time we create a new script.
Go to Edit, down to Preferences, click on External Tools, then over to External Script Editor, click the drop-down menu and select your code editor.
We’re going to initialize the player to the lower part of the screen with the following code to in the Start():
We will follow up the player’s position with creating the movement code for the player. We can start off with a basic code to move the player to the right for example:
This will move the player to the right, 5 meters per frame in real time.
Lets update this code to make the player move more to our satisfaction. We will start by creating a variable called Speed to control the speed of our player. When we create a variable, there will be some considerations we have to take into account. The following:
Public or Private- if another script needs access to the variable, make it public. Else, make it private and use the underscore symbol ( _ ) before each name variable. This will identify these variables to other programmers as private. If you want developers to be able to play around with the private values, use the [SerializeField] attribute either above the variable or in front on the same line as the variable.
Type- it will be either (int) for integers, (float) for decimals, (string) for a list of characters, (bool) for true or false answer.
Variable Name- this name should be appropriate to what value is being stored in memory.
Value- this is where you can set a value to be stored inside the variable. If you don’t assign a value to the variable, for values with numbers involved it will be assigned 0. Bool will be assigned (false), and String will be assigned an empty space “ .”
We’re going to change the player’s movement using the player’s axis. To see the player’s axis, go to Edit/Project Settings/Input Manager. Click on the Horizontal and Vertical drop-down arrows to see the keyboard keys that will control the player’s movement.
Inside Update(), we will create variables to gain access to the axis’ of the player:
We can now use this code to enhance our basic movement:
Lets update this code to make it more efficient by doing the following:
We can optimize this code again by doing the following:
Create a Vector3 variable called direction inside Update().
Now adjust the code to the following:
By optimizing the code using a new direction variable, the processor will have less calculations to make when using this code. This should be your final result:
Next time we will create borders to keep the player on screen.