Day 50: Enemy fires up at Player

Tyler Smallwood
3 min readJun 10, 2021

--

Objective: Have enemy fire upwards are player when enemy is lower than player.

Since we’ll have a new enemy, lets start off creating a new enemy script that inherits from the Enemy Script.

The next thing we’ll do is call the firing function to override what we’ll do.

Now we need to check the players position. To do this we need to turn the player variable in the base class to protected to view it.

Changed from private to protected in Enemy Class.

Back in our SmartType class we’ll do our check.

Just like how we were firing in our regular enemy, we’ll do the same but change a few items. In our instantiate function, we need to offset the position of the lasers when they fire.

The offset is 3.26f to fire out the back of the enemy.

The other thing that needs to be change is in our laser class. We’ll create another function called AssignFiringUpwards(), this function will turn a bool to true so it’ll fire in the upward direction, while not changing it’s on trigger to hit the enemy.

We use this bool in the update to decide when direction to call. Currently it’s only checking to see if isEnemy is false fire up else fire down. Now we’ll do if isEnemy is equal to false or isFiringBackwards true fire up else fire down.

To finish this enemy off, we still need to fire like the regular enemy, so we’ll use base.Firing() at the end of our function.

That’s all that is needed to have your enemy know when to fire upwards to try and hit the player still.
That is all for today! Thank you for reading!

--

--

Tyler Smallwood
Tyler Smallwood

Written by Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

No responses yet