/Unity3D – Your First Game – II

Unity3D – Your First Game – II

Hi, In this part we are going to add some interactions to the player and the environment in order to start having a game play.
If you haven’t checked part 1, please check it from here.
Here are what are we going to add to our game play: we’re going to let the player jump on platforms, shoot enemies and collect some coins.
Now, we are going to  add some lines to our previous code in order to make the player be able to jump.
Let’s check what we need to make the player jump :
– Force to move the player.
– The direction of this force which will be up.
We are going to add force towards the Y direction with Jump speed of 500.
 
The code will be as follows:

using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float walkSpeed = 10;
public float jumpSpeed = 500;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
       if (Input.GetKey(KeyCode.LeftArrow))
               this.move(Vector3.left);
       else if(Input.GetKey(KeyCode.RightArrow))
               this.move(Vector3.right);
       else if(Input.GetButtonDown("Jump")){
        GetComponent<Rigidbody2D>().velocity = Vector3.zero;
        GetComponent<Rigidbody2D>().AddForce(new Vector2(0,jumpSpeed));
       }
}
void move(Vector3 moveDirection){
   transform.Translate(moveDirection * Time.deltaTime * walkSpeed);
}
}

 
Now let’s add some platforms for the player to jump on and some coins to collect:
1
Now let’s make the player collect those coins:

void OnTriggerEnter2D(Collider2D other){
if(other.tag == "Coin"){
Destroy(other.gameObject);
}

 
Add the previous code to your script, the previous function detects if the player triggered or collided with another object or not, if the other object tag is “Coin”, then the coin will be destroyed.
 
To add a tag to your coin object:
2
Check the previous photo, to add a tag click on the Tag drop down list > Add Tag > add your tag and don’t forget to assign it to the object by choosing the drop down list and choosing the tag you have created. I’ve already online casino created my tag and assigned it to the object as you see in the photo. also, don’t forget to add collider to your object and check is trigger to true as you want to check if it triggers with another object or not.
 
Now try to play and you’ll find the coins disappear when you hit them with the player, then we will count the score but in the following tutorials as there will be a special part for the UI.
 
Now you have created a simple game play, you’ve added the jump mechanic, collecting coins and some platforms to your game, let’s add the last thing in this part which is shooting.
 
Now edit your update function to the following code:

else if(Input.GetKey(KeyCode.W)){
        GameObject shot = Instantiate(shoot, shootGun.transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
           shot.GetComponent<Rigidbody2D>().AddForce(ShootingDirection * ShootingSpeed);
       }

 
We have created an empty game object which we will initialize the shoot from it when we hit “W” button, then we will give the new shoot that has been instantiated a speed and a direction.
 
We passed the shoot and the shoot Gun as game objects from the inspector as we defined them as a public game objects in our code:

public GameObject shootGun;
public GameObject shoot;

 
 
And here are our shooting direction and shooting speed variables:

public float ShootingSpeed = 1500;
Vector2 ShootingDirection = Vector2.right;

 
 
Let’s check what have we done:
We have created an empty game object (Game Object > Create Empty) and we put it in front of the player as the place where the shoot will be instantiated(check the previous screen shoot for more explanation) every time, then we attached it to the player as it will move with the player.
3
Then, we’ve created the shoot as a prefab, a prefab means you can save an object and use it any where in the project without losing it and when you change it, it will affect every object of it, this will save your time when you get more into the game when you have a lot of objects from the same type and you want to make a change that affects all the objects from it, Also you will never lose it if it has been deleted by a mistake from your scene it will be always saved in your project.
 
Now let’s see how to create the shoot as a prefab:
Right click in project > Create > Prefab > Drag your sprite or your game object of the shoot on it.
Now you’ve created your first prefab in unity.
Here are the components of the shoot prefab:
4
 
The final form of the script with all variables and functions:

using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float walkSpeed = 10;
public float jumpSpeed = 1000;
public float ShootingSpeed = 1500;
Vector2 ShootingDirection = Vector2.right;
public GameObject shootGun;
public GameObject shoot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.LeftArrow))
               this.move(Vector3.left);
       else if(Input.GetKey(KeyCode.RightArrow))
               this.move(Vector3.right);
       else if(Input.GetKey(KeyCode.Space)){
        GetComponent<Rigidbody2D>().AddForce(new Vector2(0,jumpSpeed));
       }
       else if(Input.GetKey(KeyCode.W)){
        GameObject shot = Instantiate(shoot, shootGun.transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
           shot.GetComponent<Rigidbody2D>().AddForce(ShootingDirection * ShootingSpeed);
       }
}
void move(Vector3 moveDirection){
   transform.Translate(moveDirection * Time.deltaTime * walkSpeed);
}
void OnTriggerEnter2D(Collider2D other){
if(other.tag == "Coin"){
Destroy(other.gameObject);
}
}
}

 
 
Congratulations! Now you have a player who can move left, right, jump, shoot and collect some coins. in the next part we’ll add some enemies and animations.
See you ;).

I have a Bachelor degree of computer science. Also, graduated from ITI (Information Technology Institute), Smart Village Branch, Games Development track year of 2014. Currently working as a game developer in Software and Systems company called Fixed Solutions. Founder at 2024Studios.