Time for action – code do-si-do
Because the needs of our game have changed, we might reconsider how and where our code is written. In The Break-Up, it made sense for the player character to detect all collisions. But in Shoot the Moon, all the significant collisions actually happen on the enemy ship. The enemy ship has to know when it hits the hero ship, and it needs to know when it hits a bullet. So we can actually localize all of the collision code on the enemy ship. This makes a lot of sense, so let's make that change by transplanting some code:
In the HeroShip Script, delete the
OnCollisionEnter
function.Delete the
explosion
variable declaration from the top of the script.Save the HeroShip Script.
As we did with the
ResetPosition()
function, let's build a reusableExplode()
function, and fold the explosion-related code into it.In the EnemyShip Script, create an
Explode()
function outside and apart from the other functions:function Explode() { audio.PlayOneShot(audioClips[Random...