Game Development Reference
In-Depth Information
You may remember that we just set up the enemy and player's physics layering
to mask out these collisions. Just make sure that the Defense Collider object has
a Default layer and it will accept collisions with the player, while the rest of the
enemies continue to ignore one another—which is exactly what you want.
Now, play the game and run head-first into the enemy. What happened? The player
just died and respawned at the spawn point! Yup, it really is that easy to make 2D
games work in Unity.
You may notice that the message hitDeathTrigger has no receiver! is showing up in
the console. If you do, very good, you've got your console open! The console keeps
you up to date about errors, warnings, and messages—you should be watching it all
the time. If you aren't watching it, now would be a great time to open it up. But how
do we fix this issue? Easy; the message is showing up because now there are colliders
on the player that are hitting the death trigger. Let's just tell the death trigger that we
don't care if a receiver is there or not. Open up DeathTriggerScript and replace the
SendMessage line with the following line of code:
collidedObject.SendMessage("hitDeathTrigger",SendMessageOptions.
DontRequireReceiver);
Play again and everything works without errors!
Pro tip
Learn your event listeners and delegates! It would be quite easy to make
all the enemies laugh at the player whenever the player gets killed. In fact,
give it a shot! Add an event and event delegate object to the player, and
then add an event listener to the enemy that listens for when the player
gets killed. Make the enemies laugh like the bad guys they are!
Let's go huntin'!
The enemy can now kill the player on impact. Well, that's kind of one-sided, isn't it?
The player needs a way to fight back! If only the player had some kind of weapon.
Oh wait, they do ! Let's make the player's bullets actually do something. However, we
aren't going to edit the bullets, we're going to edit the enemy.
The enemy's Defense Collider object has a perfect size and position to also act as
the collider that takes damage. This is a case where a collider can have multiple
purposes. Let's add a new script that is specifically meant to accept damage from the
player's weapon shots. Create a new script called TakeDamageFromPlayerBullet
and write it up as follows:
using UnityEngine;
using System.Collections;
 
Search WWH ::




Custom Search