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”