My 90 day journey becoming a Unity game developer: Day-39

Objective: Fixing a bug pertaining to destroying our enemies.

When playing the game, it was discovered that we can hit the enemy twice with our lasers and score on each hit before the enemy is destroyed.

Enemy is hit twice and the player is credited in the score.

What we need to do is turn the colliders on each enemy off. We will use this simple way to fix the problem by going to our Enemy script. Inside the OnTriggerEnter2D() method and in the Laser’s IF condition, we want to destroy the collider itself on each enemy before the enemy is completely destroyed. The enemy is destroyed in approximately 2.4 seconds which means the collider on the enemy is still active until that time runs out. Right before we destroy the Enemy, insert another Destroy() method, but this time we want to get the component Collider and destroy it right away.

The collider of the enemy is destroyed before the enemy itself.

Now when we play the game, notice the lasers go through the enemy’s explosion on the second shot. Plus, look at the score and notice only 10 points given for each enemy destroyed.

Only 1 laser can destroy the enemy and receive points.

Hopefully next time I will be able to set up WebGL, because I ran into some serious errors that prevented me from building the game. If not, we will work on randomizing the enemy’s fire rate at the player.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store