UNITY | PROJECT | 2D PlatFormer | 1. Project Set Up

2
Importing Sprites

 

3
Cutting Sprites

 

4
Creating “Ground”
  • Add “Box Collider” to “Ground”

 

5
Adding “Player”
  • Add “Box Collider” to “Player”
  • Add “RigidBody2D” to “Player”
7.1
Adding “PlayerControl” Script
7.2
Physics Working

 

8
“PlayerControl” Script
  • Claim variable for Player movement
    • playerSpeed
    • facingRight as flag
    • playerJumpPower
    • movementX
  • in “Update()” method
    • get input to move horizontally by using “Input.GetAxis()”
    • get “RigidBody2D” component by using “GetComponent()”
      • call “velocity” propery to move “Player” with continuous input
    • create “if Statement” for Player’s movment
      • when pressing left and facing right
        • activate “FlipPlayer()” method
      • when pressing right and facing left
        • activate “FlipPlayer()” method
    • Create a Function, “FlipPlayer()”,
      • set the flag, “facingRight” to opposite
      • get the “localScale” property
        • multiply -1 to reverse the direction
      • Don’t forget to update “localScale”
10.2
Player Moving
9
“PlayerControl” Script
  • Create a function, “Jump()’,
    • use “AddForce()” to give force upwards to “Player”
  • in “PlayerMove()” method,
    • when “Jump” button is pressed, activate “Jump()” method

 

10.1
Input Manager

 

10.3
Player Jumping too high

 

11.1
RigidBody2D | Linear Drag | Gravity Scale
  • Set “Linear Drag” 0 to 1 to prevent “Player” from jumping too much
  • Set “Gravity Scale” 0 to 10 to prevent “Player” from jumping too much

 

11.2
Player Jumping
  • Player jumps normally but still it can jump multiple times in the air

UNITY | PROJECT | SPACE SHOOTER 2D | 6. Explosion​ Animation

2
Explosion Sprite | Sprite Editor

 

4
Explosion Sprite | Sprite Editor | Slice

 

5
Explosion Sprite | Inspector
  • For the Sprite of Explosion in the Inspector
    • Set the Sprite Mode into “Multiple”
    • Set the Filter Mode into “Point(no filter)”
    • Set the Max Sie into “1024”
6
Creating Animation from Sprite

 

7
“ExplosionAnimationContoller”
8
“Explosion” GameObject

 

9.1
Deselect “Loop Time”

 

9.2
“Explosion” Animation
  • “Explosion” Animation is appearing continuously

 

 

9.3
“Explosion” Animation

 

  • “Explosion” Animation works well except that the last frame of the animation lasts in the scene

 

10
Creating & Adding “Destroyer” Script

 

11
“Destroyer” Script
12
“Animation” Tap
13
Adding an Animation Event to “Explosion” Animation

 

14.1
Adding an Animation Event to “Explosion” Animation
14.2
“Explosion” Animation
15
Creating “Explosion” Prefab

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

UNITY | PROJECT | SPACE SHOOTER 2D | 3. Player Bullet and Player Shooting

SPACE SHOOTER 2D

3. Player Bullet and Player Shooting

3
Bullet Sprites
  • Importing “Bullets” sprite

 

5
Sprite Editor | Slice

 

  • by using “Sprite Editor”, slice “Bullets” sprites into 2 sprites
    • when slicing, set the type into “Grid By Cell Size”
    • when slicing, set the Pixel Size as 24×24

 

4

  • Having 2 bullet sprites

 

6
Sprite Setting
  • for the bullet sprite in the inspector
    • deselect “Generate Physics Shape”
    • Filter Mode into “Point(no filter)”
    • Max Size into “1024”

 

7
Adding prefab in the scene
  • Add “Player Bullet” sprite in the scene to configure like adding a script

 

8
Adding a script to Bullet GameObject
  • Adding “PlayerBullet” script to “PlayerBullet” GameObject

 

9
“PlayerBullet” Script
  • set the public variable “speed” for the speed of the bullet
  • In “Update()” method
    • get the bullet’s current position by using “transform.position”
    • computing the bullet’s new position = the change of position when we shoot the bullet
      • only need to change the “y” value
    • to smooth the shooting, use “Time.deltaTime”
    • Don’t forget to update the position into “transform.position”
  • for the memory usage, destroy the bullets going outside the screen
    • when setting the screen range, only consider “y” value or “height”
    • “Camera.main.ViewPortToWorlPoint()”

 

10
Setting the speed in the inspector | Public Variable
ezgif.com-video-to-gif
Shoot a bullet

 

11
Making a Prefab
  • Make the Bullet Prefab by dragging the bullet we configured in the Hierarchy window
    • this is the Bullet we will continuously instantiate

 

12
Empty GameObject

 

13
GameObject BulletPosition
14
GameObject BulletPosition
  • Make 2 Empty GameObject to Create “BulletPosition”, where Bullets come out
    • Set the position of  “BulletPosition” on “Player” GameObject

 

15
Child Object
  • Make “BulletPosition” as a ChildObject of “Player”
    • “BulletPosition” belongs to “Player”

 

16
“Player Control
  • Set Public Variables to use those components in the script

 

17
Public Variable | Reference

 

18
Player Control
  • In “Update()” method
    • Instantiate Bullet Prefab when “SpcaeBar” is pressed
    • set the initial position of a bullet after instantiating

 

ezgif.com-video-to-gif-2
Player Shooting | Destroying Bullets