Nerd For Tech
Published in

Nerd For Tech

Bug fixing and adding enemy fire

So, now that we have a more playable version of the game, we can look at adding some bonus features to create more of a challenge. However, first let’s look at a bug that pops up when we play. If we were to shoot at the explosion of an enemy, we can continue to gain points:

As to why this is, we would have to look at the script:

From the script, we can see that we have a delay before destroying our object, so what is happening is we are still having our laser collide with our object for 2.1 seconds after we have hit it. To solve this, we can simply destroy our collider after it has been hit by the laser and we will no longer gain points for hitting a dead enemy:

Now that we have the collider destroyed, we can check it out in our editor and see how it goes:

Now that we have this bug fixed up, let’s get some return fire going from our enemies. To start, we need to create a laser prefab for our enemies to work with:

All we simply have to do is drag an enemy prefab along with 2 laser prefabs into our enemy container. From there, we will move our 2 lasers into a position we like, and link them together by using a parent object that we call Enemy_laser. Now that we have our laser prefab built, let’s go into our enemy script and start creating the code that we will need to have our enemy fire at us. First though, we will simply create a new Void for enemy movement so that if there were an error to occur, it is easier for us to find a location of that issue:

Next, what we will have to do is create a couple floats for both fire rate and if we are able to fire. From there, we can just write our laser firing code within our void update:

To test if our enemies fire, we add the line Debug.Break(); so that once the action occurs in the game, it pauses the game so we can see how it looks on the enemy when fired:

Now that we see how it looks, let’s continue to game and see what ends up happening:

Our enemy is just killing itself with the laser. The reason is because our laser script is set up to have the laser move upwards, so we will have to go into our laser script and create a method to have it move downwards when certain conditions are met. To do so, we will create a bool statement that will have our laser move downwards, unless it is fired by an enemy unit:

As to how we get the method to become true, we have to use script communication with our enemy unit. First ,we have to create a gameobject enemyLaser so that we can call the instantiate method a lot easier. From there, we can create an array:

As we are calling on 2 separate lasers, we need to be sure we are using GetComponentsInChildren. From here, we can either type out a line of code for each individual laser, or use the for loop method we learned about. The for loop would be a much safer method to use, as if we happened to have it so the enemy shoots out 40 different lasers, we don’t need to create 40 different lines of code for that action:

Now that we have our enemy script fixed up to fire a laser downwards, let’s test it our in our game:

The final step we need to take is to allow the laser to damage the player. To do that, we will have to go back into our laser script and add a ontrigger event for our laser so that when it hits our player, we know that we want the player to take damage:

Now that we have the damage script connected, let’s test it out in our game:

There we go. Our player takes damage from the laser, and in this case it is 2 points worth of damage because of how we have it set up. As we are being hit by 2 separate lasers, it makes sense that we are taking 2 points of damage. We could change it to 1, but as this gives an added difficulty layer to the game, we will leave it in place.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Requests & Limits recommendations using VPA, Goldilocks and Grafana

BurgerSwap just got upgraded! What’s new in the new version?

Open Source Hack Experience: Mentorship Android

Google Play Store Hidden Facts

Java 8 | String Method Practice

6 Useful Firebase Services For Mobile Application Development

6 Useful Firebase Services For Mobile Application Development

How to build a Powershell GUI — Part 2

What is a STOSA Organization?

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

Jordan Evans

More from Medium

Optimizing the Animator and Callouts

Scaling Bar HUD for Thrusters

Doing basic unit testing in Unity/C#

[ SDL2 — Part 7] Loading png files