UNITY | PROJECT | 2D PlatFormer | 2. Camera & Death

2
“CameraSystem” Script
  • Create Variables for Player, and Camera’s Scope
  • in Start() method,
    • find player by using FindGameObjectwithTag()

 

3
“Main Camera”
4
“CameraSystem” Script
  • in LateUpdate() method,
    • create x & y variable for the position of Main Camera
      • Put the x & y values of Player in those x & y
      • Clamp the x & y with min & max values
        • Main Camera will move based on Player‘s Position
        • when Main Camera reaches at the position of Min or Max Values, Main Camera stops there
4-1
Main Camera following but clamped

 

5
Adding Box with Box Collider

 

6
Adding Ground2 with Box Collider
6-1
“Box”

 

2. Camera - Death
Creating “Hole” with “Box Collider”
8
Creating and Adding “PlayerHealth” Script

 

9
“PlayerHealth” Script
  • Create died bool variable as Flag
  • in Start() method,
    • Set the Flag as false
  • in Update() method,
    •  if Player‘s y position is below than -7,(Player falls down), show the log

 

9-1
Player Falling
10
PlayerHealth Script
  • Create a Coroutine
    • Create Die() as IEnumerator
    • Start the Coroutine with Die() by putting StartCoroutine() in Update() when Player died
    • don’t forget to put yield and return in IEnumerator
10-1
CoRoutine Working
11
“PlayerHealth” Script
  • change IEnumerator Die()
    • load the main scene when this IEnumerator is called by using SceneManager.LoadScene()
    • Don’t forget to yield return null
12
Scene Loaded well

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