[Question] Lecture 110 - Shooting Enemies?

At around the 1:30 mark in this Lecture we are asked to:

  1. Log a message when a projectile hits an enemy.
  2. Bonus points if you can figure out how to log messages only when hit with projectiles

Well, I am pretty sure that I can do the first one. I’ve done it before in this course when expanding upon the Block Breaker game. But I cannot figure out what he is actually asking in the second question! So, I’ll give bonus points to anyone who can explain what he wants us to do?

Like is he asking us to send a message or access a method in the enemy controller class saying that a hit has occurred OR is he asking for us to setup a collision system in the enemy controller class itself as well OR is it something else entirely?!?

Any help is as always greatly appreciated!

Regards,
Vaughan.

1 Like

it reads to me like it should check the collision to make sure it’s a projectile (rather than a ship) before logging. So only log if it’s a projectile hit.

1 Like

Yeah … but, if you put the sentence structure in Yoda style you get a different message:

“When hit with Projectiles, a log message create”

So, to me it seems that he wants the enemy to also have a collision system. But then that would be doubling up on the collisions, only one of the objects needs to do it! And as far as I know only one of the objects will report the collision - even if both objects have it scripted. I just wish it was written without ambiguity.

Oh well, I’ll get on with number 1.

1 Like

What if you want to do something cool like ships colliding with each other when hit by a projectile? One ship hit by a projectile causes it to spin out of control, it hits another ship in its path and causes it to take damage (for example). This gives you more flexibility in your game by creating a collider for projectiles and a collider for enemy ships.

1 Like

If you want ships to collide with ships then you will need to add code to the ship also.

Assuming the following scenario;

Player ship
Enemy Type 1
Enemy Type 2

Player ship shoots, hits Enemy Type 1, causes damage
Player ship shoots, hits Enemy Type 2, causes it to fly out of control and hit other ships

For the latter, you would need to have some code to detect a collision, and further more to distinguish the type of collision, in this case, a projectile from the Player ship.

You would want to be checking the state of this variable to see for when it changes so that you could then act accordingly, in this case, perhaps initiating a separate animation.

You would need to determine what this enemy ship could influence, for example, if it hits other Enemy ships, can it cause damage, can it cause other Enemy ships to go out of control, can it cause damage to the player ship.

With the above, it could be enhanced so that the enemy ships had some form of shield or hull strength, and perhaps they only spin out of control when this is depleted, this would then allow you to have an out of control ship to potentially cause damage to everything it collides with. It should also absorb any other projectiles, from enemies or the player, doing so could perhaps then just destroy it, giving the player an opportunity destroy an out of control enemy ship before it creates chaos.

All of the above could be built up in little steps, adding a script to an enemy ship, just having output something initially for proof of concept, adding differing behaviour for different Enemy Types, and then adding the additional animation, collisions with other projectiles and so on.

It’s a nice idea Niki - would be good to see someone implement this. :slight_smile:

I think you’ll find that if you add code to both GameObjects to detect a collision, it will be reported back through the OnCollisionEnter2D method within both GameObjects, this would allow you to handle the event accordingly depending on the GameObject, without them necessarily needing to know about each other specifically. For example, the projectile might just destroy itself and disappear from the game, the ship may flash to indicate a hit and reduce a value from its own health.