Player Movement
I wanted the player to be able to move left and right and fire laser beams towards the asteroids. To do this;
First of all, I created a script called "PlayerController.cs" and attached it to the spaceShip "Player"
Then, I measured the left and right bound of the screen and stored it in the variables
xLeftBound = -22
xRightBound = 22
Decide what Input to get from User: I decided to use "Horizontal Input" which takes input from the user if they press "Left and Right" or "a and d" in the keyboard. The input was stored in a variable:
horizontalInput = Input.GetAxis ("Horizontal");
You can review the default Input Settings that Unity has by going to "Edit --> Project Settings --> Input Manager"
Moving the Player Left-to-Right
Once we get an input from the user, we can move the player using transform.Translate (DOCUMENTATION)
In this game, spaceship will be moved left to right, i.e. X-direction. So the new Vector3 should be (1, 0, 0)
Since we have horizontalInput, we can replace the x-value of new Vector3 coordinate to horizontalInput.
I multiplied the coordinates with Time.deltaTime and moveSpeed.
Time.deltaTime allows the movement to be unit/second instead of moving the object frame to frame. The fps of the system varies based on the device user is using. Also, the fps of the system is inconsistent. So, to get a smooth movement of the player per second, we multiply the movement by Time.deltaTime.
Setting up Player Inbounds (DECISION MAKING in C#)
To set the player inbound, I used a decision making "if" statement
My condition for the player was:
If (player's x position < xLeftBound)
then, decide player's x position = xLeftBound
Similarly, if (player's x-position > xRightBound)
then, decide to set player's x-position = xRightBound
This helps to keep our player within the Screen.
PROGRESS SO FAR...