# Listening to Hit Events

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:

![Main camera, floor & cube objects.](https://2062757973-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MaImivuu1fX5yKbO7hR%2F-MaK_84n70iYwZxTya2T%2F-MaK_m3R3RycwRbXAECS%2Fimage.png?alt=media\&token=56e71962-70fa-4422-895c-fcd9359d72e8)

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.

{% hint style="info" %}
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.
{% endhint %}

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:

{% embed url="<https://gist.github.com/b0fb425e5bd83e3f28c3dc280cf59af8.git>" %}

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.&#x20;

{% embed url="<https://gist.github.com/22afbefb69ae12a7f19188320afe699b.git>" %}

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.

{% embed url="<https://gist.github.com/a617114f862cdb8629e929ab53953e99.git>" %}

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:

{% embed url="<https://gist.github.com/a9ee3804d9158530ab08eedc907425b3.git>" %}

Now when you play the game, hit space key and watch the console, you should see the output:

![](https://2062757973-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MaImivuu1fX5yKbO7hR%2F-MaKaMpeNeEJq4aXuAjz%2F-MaKbxzy-JYX32n9W5Tt%2Fimage.png?alt=media\&token=d2ded937-cfe4-4559-b701-82fb0512583b)

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.
