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 | 4. Enemy Animation & Enemy Spawner

2
Creating Enemy Animation
  • by selecting multiple Sprites and adding them in the scene, Create “EnemyAnimation”

 

3
Changing the name of EnemyAnimationController

 

4.1
“Enemy” GameObject

 

4.2
“Enemy” GameObject with Animation

 

5
Adding “EnemyControl” Script to “Enemy” GameObject

 

6.1
“EnemyControl” Script
  • Set the speed of Enemy with a float variable “speed”
  • in “Update()” method
    • change the position of “enemy” GameObject with “transform.position” every frame
    • for memory usage, destroy “enemy” when it goes out of the screen

 

6.2
“Enemy” Moving

 

6.3
“Enemy” Destroyed when outside of the screen

 

7
Creating “Enemy” Prefab

 

8
Creating “EnemySpawner” & Adding “EnemySpawner” Script
  • Create Empty GameObject and change its name into “EnemySpawner”
  • Create “EnemySpawner” Script and add it to “EnemySpawner”

 

9.1
“EnemySpawner” Script
  • To use “Enemy Prefab” in the script as a component
    • add “Public GameObject” variable
    • Don’t forget to add the reference in the inspector
  • Create a custom Function, SpawnEnemy()
    • Instantiate()
    • after instantiating, set the position of Enemy in a random position by using random range of “x” value
  • To make it spawn in 5 seconds

 

9.2
Enemy Spawning

 

10
Don’t forget to add the reference

 

11.1
“EnemySpawner” Script
  • Create a function, “ScheduleNextEnemySpawn()” to spawn multiple times
    • by using, “Invoke()” and putting “ScheduleNextEnemySpawn()” inside “SpawnEnemy()”
      • without using “loop”, we can make enemy spawned continuously
  • To increase the difficulty of the game, Create a function, “IncreaseSpawnRate()”

 

11.2
Spawning Enemies