Spawning Particles on Hit
Last updated
Last updated
As mentioned before, RSB works with Object Poolers to manage object spawning. So it's a good idea to stick with the same methodology in our tutorials. Let's create a new empty game object, name it to Default Hit Pooler, then attach the ObjectPooler.cs script on this. By clicking to the prefab selection button next to the Pooled Object field, select the Hit Effect Metal prefab.
Now, let's add a field in our Tutorial.cs script to reference to this pooler.
Then, in our OnAnyHit method, we will get a new pooled object. Place it to the hit position, make it look at to the hit normal, and enable it.
Do not forget to assign the defaultHitPooler variable on inspector of our Tutorial object.
So, now our whole Tutorial.cs script should look like this:
Then, if we play and shoot, we will see the default dirt particle is spawned when the bullet hit's the target.
The hit particles have a deactivator script attached to them. So after they are enabled, they disable themselves after a specific time has passed. As soon as they are disabled, they become available to use in the object pool.