Instantiating a Spatter Effect
Apologies for the slightly more graphic topic for this article, but I recently worked on some logic to instantiate a blood spatter effect on enemies within this FPS project (wherever the player clicks/shoots). Essentially, after detecting a raycast collision, I call an ‘InstantiateBloodSpatter’ method, passing in a RaycastHit struct variable.
The InstantiateBloodSpatter method first picks a random GameObject prefab from a serialised list of prefabs, creating an element of randomisation to the effect. I then Instantiate and create a reference to an instance of this selected prefab, spawning it at the position of the RaycastHit.point + the RaycastHit.normal *0.01f. I personally found the spatter effect was clipping through the surface of the enemy otherwise, so this ‘+ RaycastHit.normal’ part just seemed to yield some fairly neat results for me. Also, to get the right rotation I needed to specify a Quaternion in the Instantiate method, specifically using Quaternion.LookRotation(hit.normal)). This sets the rotation of the instantiated spatter so that it appears to lie flat against the surface it’s instantiated on.
Finally, I made two extra additions to the method:
- I’m setting the Transform.localScale of the instantiated spatter to half of its default size (although you could just choose to change the size of the spatter prefabs themselves).
- I’m setting the parent of the instantiated spatter to RaycastHit.transform (i.e. the Transform of the object hit by the ray).
I’ll include the end result in Unity, as well as the InstantiateBloodSpatter method below in case it’d be of use 😉