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 | 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