Listening to Hit Events
Last updated
Last updated
Let's first create a new plane object, place it in position 0,-1,0 and scale it to 5,1,5. This will act as our floor. Then create a new cube object, place it to 0,2,10 and scale it to 5,6,2. Remember our main camera was positioned at 0,0,0. The scene should look like this:
Now if you hit space key, you will see that the bullet hits the cube, and a green ray is casted upwards from the hit point. This is because Draw Hit Rays checkmark should be enabled in SniperAndBallisticsSystem.cs instance.
In order for raycasting to detect objects, your objects need to have proper colliders on them. By default, the cube we have created will have a box collider attached.
Next, open up our Tutorial.cs script. RSB provides a robust event system to understand what’s going on during the simulation. There are 5 events we can listen to in order to get information about what was hit and how:
EPenetrationInHit: Called when the bullet hit’s a surface that it has started penetrating.
EPenetrationOutHit: Called when a bullet leaves a surface that it was penetrating.
ERicochetHit: Called when the bullet ricochets off a surface.
ENormalHit: Called when the bullet hits a surface, doesn’t penetrate or ricochet.
EAnyHit: Called for all hit types, this is what you will use in most cases.
In order to listen to these events, we first need to register to them. All the events send a single parameter of type BulletPoint, which is a class defining a point in bullet’s travel path. It contains information such as the hit type, hit transform, normal, bullet’s velocity, kinetic energy etc. In order to register to an event, we first need to have a method in our class that corresponds to the event’s signature. In somewhere empty in our Tutorial.cs script, let’s create a new method:
Then we need to register this method as a callback to the EAnyHit event. It is a good idea to register to events in OnEnable() and deregister them in OnDisable() functions.
Now doing so, whenever something is hit by the simulation raycasts, our OnAnyHit method in Tutorial.cs will get called with information about the hit. Let's add a Debug.Log to print out some hit information.
Her, we use the m_endPoint property inside BulletPoint reference to learn the hit position, then also print the hit type using m_hitType property. Now, our whole Tutorial.cs script should look like this:
Now when you play the game, hit space key and watch the console, you should see the output:
By listening to hit events, you can for example write a hit listener that will apply force to the hit objects who has a rigidbody. An example of this can be found in HitListenerApplyForce.cs script under InanEvin>Realistic Sniper and Ballistics>src>Hits folder. Moreover, using the powerful structure provided by events, it is possible create particle managers who are responsible for listening to hit events, checking the hit object's tags or layers and spawning the appropriate particle effects. An example of this can be found in the same folder, in the HitListenerSpawnParticle.cs script. But for the sake of our tutorial, let's do this ourselves in the next page.