Daily Progress: Adding a bit ‘o blood.

While we’re now able to shoot our zombies and through our health system damage and kill them, there’s no visible feedback. Todays lecture focused on that.

First we downloaded some blood splatter from filebaseHQ into our assets.

Next, we were given the challenge to instantiate the blood splatter prefab onto the enemy when we hit. We’re to research hitinfo.normal as well as hitnormal.position in order to rotate the splatter appropriately on the model.

To begin with, I think we’re going to need to create a holder for our bloodsplat prefab so I’ll create a variable in our shoot script since that’s where we cause damage and our raycast code is used:

In Shoot:

Now we’ll drag the bloodsplat into the script. Bloodsplat 2 is the recommended one so that’s what I’m going with. I thought I would drag it into the script but that wouldn’t work since the bloodsplat wouldn’t instantiate later, so drag it into the player where the shoot script is at.

And here’s the code I came up with with a bit of googling and youtube searching:

In Shoot/FireGun:

So before we access the enemys health.Damage method, we’ll Instantiate the blood splat at the raycasts hit.point, and we’ll need to use Quaternion.LookRotation to translate the hit.normal (the reflection angle of the surface of the object). That way, the bloodsplat will always be in the correct angle of the surface at the time we shoot it.

We place this instantiated blood splatter into a variable called bloodSplat which we then destroy after 1 second. Let’s see it in action:

We’re getting bloodsplats, but they seem to be off, turning off the destroy function, we see what’s happening is they’re actually instantiating on the colliders border.

Our raycast will have to ignore the sphere collider. It was figured out that we’re going to have to refactor our enemy by essentially making the collider a child via an empty and then create a layer that the raycast ignores.

First we’ll remove the sphere collider from our enemy.

Next we’ll add an empty child to our enemy and add the sphere collider to that.

Create a new layer called EnemyCollider and put the child collider holder into it. Remember to set it as a trigger.

I initially named the Empty and Layer ColliderHolder, but AttackTrigger is a more appropriate and descriptive name, so you may want to change it to that.

Our parent enemy seems to be in the same layer, it may not have happened to you, but if it did, go ahead and set it back to default, obviously say no to changing the children.

We’re going to have to move the OnTriggerEnter and OnTrigger Exit code from the enemyAI code to a new script called AttackTrigger that we’ll attach to the AttackTrigger empty and then figure out a way to communicate the state to our enemy.

Let’s create the AttackTrigger script and attach it to the AttackTrigger enemy object.

Next we’ll move the OnTrigger code from the EnemyAI script to the OnTrigger script

We’re going to create a holder for our EnemyAI script.

In AttackTrigger

We won’t be able to affect our current state, so we’ll need to create some functions in the enemyAI to do that:

in EnemyAI

Now all we need to do is call them in the AttackTrigger:

Let’s give it a go!

Working wonderfully! Now let’s deal with the raycasting ignoring the trigger:

First, make note of what layer our attack trigger (Or collider holder) layer is on, in our case, it’s layer 8.

In our shoot code, we’re going to add a few more options like distance and a layer mask to our raycast so it ignores the attack trigger layer:

The 1<<0 is known as bitshifting, we’re shifting the bit (1 and 0) in layer 0 so that it’s on/1. This tells unity to only look at the default layer and ignore everything else essentially eliminating our sphere collider!

Let’s give the enemy its own layer, 9.

While we’de like to focus on the enemy, let’s include the default layer as well.

And we’re successfully shooting the default and enemy layers!

It looks like now that we’re actually shooting our enemy and not its collider, that the shots aren’t completely on the surface, let’s expand the enemies character controller radius a little to around .85

And we’ll also de-comment our bloodsplat destruction. I shortened the time a smidge shorter than 1 second.

Let’s see what we’ve got!

Hooray! We’ve upgraded to an M rating! :D We’ll get into Character IK next.

cartoonist, game artist, and wanabe gamedev.