UNITY | PROJECT | 2D PlatFormer | 7. Save & Load

2
“EndLevel” | Sprite Renderer

 

5
“EndLevel” | “DataManagement” Script

 

 

3
“DataManagement” Script
  • in Awake() method,
  • create a function, SaveData(),
    • make a BinaryFormatter to serialize the data later
    • make a FileStream and File to create a path and the target file
    • create a container for the save data by creating another class in the script
      • put the data in it
      • serialize the data with the BinaryFormatter
      • Don’t Forget to close the file by using File.Close()
  • create a function, LoadData(),
    • make a BinaryFormatter to Deserialize the data later
    • make a FileStream and File to open the target file
    • Deserialize the data with the BinaryFormatter
    • Don’t Forget to close the file by using File.Close()

 

4
“PlayerScore()” Script
  • Create a function, CountScore()
    • show the current score with Debug.Log()
    • calculate the score
    • update the score
    • Save the Score by using SaveData() function from DataManagement Class
    • show the updated Score with Debug.Log()
  • in Start() function
    • put LoadData() function from DataManagement Class

 

UNITY | PROJECT | 2D PlatFormer | 6. Enemy

2
“PlayerControl” Script
  • with the rayCasting downwards, detect Enemy
    • Kill Enemy
      • enter Enemy‘s properties to modify gravityScale, FreezeRotation, BoxCollider2D, EnemyControl
2-1
Killing Enemy

 

3
“EnemyHealth” Script
  • when GameObject(=Enemy)’s position is lower than -10 in y
    • destroy Enemy

 

3-1
“Enemy” Destroyed

 

6. Enemy 1
“PlayerControl” Script
  • Crate another RayCast upwards
    • if detecting BoxSpecial, Destroy it

 

5
Creating “BoxSpecial”
  • add Box Collider 2D

 

5-1
Destroying Box

UNITY | PROJECT | 2D PlatFormer | 5. Collectables

2
Creating “Coin”
  • create Coin
    • add Circle Collider 2D
      • make it Trigger Collider by checking Is Trigger
    • add RigidBody2D
      • make it Static Collider

 

3
“PlayerControl” Script
  • in OnTriggerEnter2D() method,
    • if a collision with Coin happens
      • add score by 10
      • destroy the gameObject itself (=Coin)
    • OnTriggerEnter2D vs OnCollisionEnter2D
      • When there are TriggerColliders, use OnTriggerEnter2D
      • When there are normal Colliders, use OnCollisionEnter2D

 

3-1
Collecting Coin Working

 

4
ProjectSetting | Physics2Dsetting
  • Uncheck “Queries Start in Colliders”
    • I guess Query there means anything to detect in physics such as RayCast
    • with that option checked, RayCasting starts from the inside of collider attached to GameObjects
    • with that option unchecked, RayCasting Starts from the collider attached to GameObjects

 

5
“Enemy” Inspector
  • Tag as “Enemy”
  • Set the layer as “Default”

 

6
“PlayerControl” Script
  • create a function, PlayerRayCast(),
    • attach RayCast by using RayCastHit2D, and Physics2D.RayCast(vector2 origin, vector2 direction)
    • create an if statment,
      •  give Player jump by using GetComponent<RigidBody2D>.AddForce()

 

7
“PlayerControl” Script
  • Add PlayerRayCast() function in Update() method

 

7-1
Jumping on “Enemy”

 

8
Experiment | Layer : Ignore RayCast
  • put Enemy back in IgnoreRayCast Layer

 

8-1
RayCasting Not Working
  • with the option, “Queries Start in Colliders” unchecked, Enemy is moving correctly because RayCasting Starts from the exterior of Collider attacked to Enemy
  • with the fact that Enemy belongs to Ignore RayCast layer, the RayCasting feature is not working

 

9
“EnemyControl” Script
  • in Update() method,
    • with the Raycasting feature, Enemy detects other objects horizontally
    • in the horizon, if Enemy collides with Player, Destroy Player

 

9-1
Jumping on “Enemy”

 

10
“PlayerControl” Script
  • in PlayerRayCast(),
    • set if statements with the conditions,
      • if Player hit anything, and if it is Enemy, make Player jump on Enemy
      • if Player hit anything, and if it is not Enemy, set the flag for jump as true
10-1
“Player” can jump anywhere but “Enemy”

 

 

UNITY | PROJECT | 2D PlatFormer | 4. Score System

2
“PlayerScore” Script
  • create timeLeft & playerScore
  • in Update() method,
    • make timeLeft pass by using Time.deltaTime
    • create an if statement
      • when the time reaches approximately 0, load the scene again
  • in Awake() method,
    • temporarily, set the time into 5
3
“Player” Inspector

 

3-1
Time is passing

 

4
Creating “EndLevel”
  • create EndLevel from empty gameObject
    • add Box Collider
      • check is Trigger to make it as Trigger Collider
    • add RigidBody2D
      • set Body Type as Static
        • to make the gameObject immovable
        • the least resource-intensive body type
        • only collides with Dynamic RigidBody2D
        • Having two Static RigidBody2Ds colldie is not supproted
5
“PlayerScore” Score
  • create a Function, CountScore(),
    • update playerScore by calculating with timeLeft
    • create OnTriggerEnter2D()
      • put CountScore() in to make it activated when collisions happen

 

5-1
End Level
6
Creating UI

 

7
“PlayerScore” script
  • create GameObject variables for UIs
  • in Update() method,
    • use text property in UIs by using GetComponent()

 

7-1
UI Working

UNITY | PROJECT | 2D PlatFormer | 3. Basic Enemy

2

3
“PlayerControl” Script
  • Create a bool variable isGrounded as Flag
  • Create OnCollisionEnter2D() method,
    • when a collision with an object tagged “Ground”
      • the flag is set to “true
4
Tagging “Ground”
4-1
Collision Detection Working
5
“PlayerControl” Script
  • in Update() method,
    • add code as a condition for Player to jump
    • when the button is pressed and at the same time the flag is true(Player is grounded)
  • in Jump() method,
    • add code to set the flag to false
6
Box not tagged
7
Ground Tagged

 

9-1
Jump
  • Player is not jumping on Box because Box is not tagged as Ground

 

 

8
Creating “Enemy”
  • Create Enemy
    • add BoxCollider
    • add RigidBody2D
    • add EnemyControl Script

 

9
“EnemyControl” Script
  • Create variables for Enemy‘s speed and direction
    • Enemy is moving horizontally only, so take care of X
  • in Update() method
    • access velocity property to move the gameObject by using GetComponent
10
“Enemy” Inspector | Public Variables

 

11
“EnemyControl” Script
  • in Update() method,
    • add RayCasting to perceive the obstacles
    • Physics2D.RaycastHit2D(vector2 origin, vector2 direction)
      • the origin where Ray is cast from is the current position of gameObject
      • the direction of casting the ray is only x
      • when the length of ray is less than 0.7,
        • activate Flip()
    • create a function, Flip(),
      • change the direction in x opposite whenever this function is called
12
“Enemy” Inspector
  • put Enemy in Igonore Raycast layer
  • set the speed

 

12-1
Enemy Moving

 

13
When layer is defualt

 

14-1