Comment on page
Firing a Bullet
In order to fire, we need to call a single method on SniperAndBallisticsSystem instance. First let’s create an empty game object and name it Tutorial. Then create a new script, name it Tutorial.cs and then attach this script to the Tutorial object in your scene. Open up the Tutorial.cs script. In order to use RSB classes, we first need to include a using IE.RSB directive at top of our script.
Then, inside our class, we will need to reference the bullet properties asset file we’ve created to use it while firing.
In order to be able to fire a bullet, we first need to activate it. Activating a bullet triggers a set of functions to pre-calculate some bullet variables such as it’s drops or zero angles, which are needed during
simulation. By activating the bullet first, we avoid calculating these variables in runtime, saving us a performance. To do so, we will need to call a method in SniperAndBallisticsSystem instance. This is a singleton instance, meaning that it can be accessed from anywhere in your scene as long as one instance is attached to an object in the scene, which is the case for us.
It is a good idea to activate the bullets in Start function, since we make sure SniperAndBallisticsSystem singleton instance will be definitely available to use in Start. We call the ActivateBullet method, pass in our bullet properties instance. You need to do this in start function for every type of bullet property you plan to use in your scene. Usually this will be only for a single bullet, but for instance if your game has different ammunition on different weapons, you can write a script that will create a list of bullet properties references and iterate over them to call the function above in Start for all of them.
Next, let’s fire this bullet! Let’s setup a block in our update so that we can fire when we press Space key.
Now, only thing we need to do to call FireBallisticsBullet method to fire our bullet defined by myBulletProperties.
And that’s it! Now if we go to the Tutorial object we have attached this script, and drag & drop our bullet properties asset file as the reference to myBulletProperties variable, we will be good to go!
If we play the game and hit space key the system will fire from the main camera, because that was what we have attached as the Fire Transform variable in SniperAndBallisticsSystem instance. We can see the bullet trajectory in scene view since we have enabled ray debugging.
Scene view showing the trajectory of the bullet fired from main camera.
We have fired our first bullet which is ballistically simulated! Now let’s learn how to listen to hit events for this bullet, so that we can cast particles, or apply forces to rigidbodies that are hit.