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