Nerd For Tech
Published in

Nerd For Tech

Let’s get some Rammers

The first new enemy type that we are going to look at introducing is a ramming style of enemy. In order to do this, we need to first create a variable that will determine what distance we want our enemy to start ramming us from and then a new speed we would want them to move at. Since we will be making a few new enemy types, we are going to create a new void for our enemy types to be determined from by the use of a switch statement, like we had done previously with the movement type:

From here, we can go into our void start and set _enemyType = Random.Range(0,2) for now. As we slowly add in new enemy types, we can increase the range that we want the numbers to be chosen from. In the end, we will set up our switch statement just like how we set up our powerups, but for now we will keep the spawn odds even across the board.
Next, we are going to take the fire method that we created for our enemy, as seen here, and add it to both our new ramming enemy along with a new void for our base enemy:

Next, we will create a method with our new detection range variable that will tell Unity that when the enemy gets within a certain distance of our player, we want it to start trying to ram our player. As we are increasing the speed, we need to be sure to go into our death parts of the script and add in _ramSpeed = 0 to all the methods so that we don’t have a floating ball of fire chasing after our ship if we destroy it before it rams us:

Next, all we will need to do is call our EnemyType() method in the void update and we can test it within our game:

As we can see, the enemy will chase after our player until the distance is too great for it to follow, then it will just resume it’s path downwards. Now that we have 1 new enemy type in place, we can look towards adding more in the future.

--

--

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