UNITY TUTORIAL | PROJECT | SPACE SHOOTER |9. Creating Hazards

UNITY TUTORIAL | PROJECT | SPACE SHOOTER |9. Creating Hazards

2
Adding Asteroid as Hazard

 

3
Adding “RigidBody” and “Capsule Collider”

 

4
adding a script to give Tumbling
5
setting tumbling speed with a public variable
6
RigidBody.AngularVelocity | Random.insideUnitSphere
ezgif.com-video-to-gif
tumbling Asteroid but with Angular Drag
7
setting Angular Drag to 0
ezgif.com-video-to-gif-2
Asteroid tumbling without Angular Drag
8
adding a script to make destruction
9
OntriggerEnter to detect any collision | Destroy()
  • Destroy(other) – only removing the collider
  • Destroy(other.gameObject) – removing only the gameObject with Collider “other”
ezgif.com-video-to-gif-3
Bug | something is triggering “Destroy()” function
10
Debug.Log (other.name)
ezgif.com-video-to-gif-4
Debugging 1 | finding the cause
11
Debugging 2 | finding the cause
12
Debugging 3 | Tagging “Boundary”
13
Debugging 4 | detecting any gameObject with a tag “Bounadry” | return
  • Return ends the execution of a function and hands the control back the function that called it (maybe Unity itself)
  •  Return ends OntriggerEnter Function and returns the control back to Unity’s Game Loop
  • As a result, if the object’s tag is “Boundary”, we will never reach or execute “Destroy” function
ezgif.com-video-to-gif-5
Debugging 6 | Solved
ezgif.com-video-to-gif-6
Asteroid destroyed by Laser