Actually Hit Enemy!- Game Dev Series 127

S.J. Jason Liu
4 min readAug 31, 2021

Objective: create blood splash effect on Enemy where was hit by Player.

Since we can shoot enemy with crosshair, now would be a good time to create some effect on it. In this article, we will create a blood splash effect on enemy.

Blood splashing animation

First, we need the gameobject of blood splashing animation. Here I would use the blood splashing gameobjects from GameDevHQ.

With your blood animation, open Shoot script and create a gameobject variable to store this gameobject. Then drag the gameobject into script as reference.

Now the blood splash is ready, let’s hit the enemy with it.

Instantiate blood on enemy

In the previous article, we have created mouse left click to attack enemy. All we need to do is to instantiate a blood splashing animation onto enemy after we clicked.

To instantiate a prefab on enemy, we will need 3 datas.

  • prefab to generate
  • position to generate
  • rotation of generated prefab

The first one, we have imported the prefab and set the reference to script.

About the position, as this is a raycasthit target, we can simply use “RaycastHit.point” as the position of raycast hit.

And the rotation would be the normal of raycast hit, which we can use “Quaternion.LookRotation()” to do this part.

With this line, we can create a blood splashing animation on enemy.
However, while enemy is chasing Player, the blood animation would only generated on the position of enemy in the last frame. We need to make the blood animation stick on enemy and disappear after a while.

We can set the generated gameobject as a child object of enemy, and that would make the prefab stick on where it was hit.
Then use Destroy() to clean the prefab.

Now you can test the effect.

Ignore trigger collider

Once you tested the blood splashing effect, you should noticed that blood would generate on the sphere collider of enemy.

That is because we use a sphere collider to detect the Player. To prevent this error, we need to use QueryTriggerInteraction.Ignore to make the raycast ignore the trigger collider.
And to use this condition in Raycast if statement, we also need to set the layer mask and casting distance.

Let me quick explain about this line of code.
First added content would be casting distance. In here I use “Mathf.Infinity” to make it no distance limit on raycast.

The next would be layer mask. We have used this in the previous article, and here I switch the layer mask to the very first of layer to make to allow to any objects.

The final part is to make this raycast ignore trigger collider. While we ignore the trigger collider, the raycast would hit on the character controller of enemy.

Back to editor, we can slightly increase the radius of character controller, to make the blood splashing animation more obviously.
Or if you want to have a clear display of blood animation, you can create another non-trigger box collider to it to check the result.

Slightly gore but it is a good way to practice layer mask and raycast.

--

--

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.