UNITY | PROJECT | SPACE SHOOTER 2D | 5. Enemy Bullet & Enemy Shooting

2
Adding “EnemyBullet” Sprite to the scene to configure
3
Creating & Adding “EnemyBullet” Script
4
“EnemyBullet” Script
  • Create Variables for “speed”, “direction”, “flag”
  • Create a Function to set the Bullet’s Direction, “SetTheDirection(Vector2 direction)”
    • Be careful not to be confused with the names of variable “direction”
      • to differentiate them, put “this.” in front of “direction”(not the argument)
    • normalize the vector to get purely a direction (to make the computing correct and simple)
    • set the flag(the boolean variable) as “True” to activate the codes in “Update()” method
  • In “Update()” method
    • the codes will be activated when the flag is set to “True”
    • the position of the bullet is updated every frame by calculating “transform.position” with “direction(Vector2, the result from “SetDirection()”)’
    • if a bullet goes out of the screen, Destroy it
  • In “Awake()”
    • every code in “Awake()” is activated before the game starts
    • before the game starts, set “speed” and “ready” with default values

 

5
Creating “EnemyBullet” Prefab

 

6
Add “Enemy” Prefab temporally to configure

 

7
Adding “EnemyGun” to “Enemy” by creating Empty GameObject | Adding “EnemyGun” Script

 

11
“EnemyGun” Script
  • Add a public GameObject variable to use EnemyBullet prefab as a component
  • Create a Function to fire bullets from Enemy, “FireEnemyBullet()”
    • get a reference of “Player” by using “GameObject.Find()”
    • the way of detecting a GameObject, “if(gameObject != null)”
    • if “Player” is detected, a bullet is instantiated and its initial position is set
    • how to calculate an aiming direction: Vector – Vector
    • to use “SetDirection()” method of “EnemyBullet” Class, use “GetComponent()” towards “bullet”
  • In “Start()” method
    • make “FireEnemyBullet()” activated after 1 second, by using “Invoke()”

 

12
Don’t forget to add the reference in the Inspector

 

13
Applying Changes to Prefab
  • To apply changes on the prefab, Don’t forget to push “Apply” button in the Inspector

 

14
Enemies Spawning, Moving, and Shooting

UNITY | PROJECT | SPACE SHOOTER 2D | 2. Player Input Movement

SPACE SHOOTER 2D

2. Player Input Movement

2
Player Control Script
  • Creating & Adding “PlayerControl” script

3
Player Control Script | Update() | Input.GetAxisRaw()
  • setting “speed” variable as public to see in the inspector
  • In “Update()”
    • set horizontal & vertical input by using Input.GetAxisRaw()
      • with Input.GetAxisRaw(), the values will be -1, 0 or 1
    • with the two inputs, make “direction”
      • by adding “.normalize”, make a unit vector
    • add a customized fuction “Move()” to move “Player”
      • we need this fuction in “Update()” because “Player”‘s position must be changed and detected every frame

 

4
Player Control script | Move() | Vector2 | Camera.main.ViewportToWorldPoint()

 

5
Player Control script | Move() | transform.position | Mathf.Clamp()
  • get the Player’s current position with “transform.position”
    • transform.position of the gameObject this script is attached to
  • calculate the new position in “currentPosition” variable
    • with “speed” variable, we can change the speed of “Player”
      • “direction” is the normalized vector2
      • “Time.deltaTime” is very important with this, we smooth the movement
2. Player Input Movement
without “Time.deltaTime”, the value(“direction”) is almost (0, 0), (1, 0)……

 

6
Setting Player’s Speed | Public Variable

 

1
Player Input Movement
2
Player Input Movement | without Time.deltatime
  • without “Time.deltaTime”, “Player” moves too dynamically