Sniper and Ballistics System
Last updated
Last updated
The central piece of RSB is a SniperAndBallisticsSystem singleton instance. Without it, you can not use the ballistics simulation. Create an empty game object, name it Sniper and Ballistics and attach the script SniperAndBallisticsSystem.cs on it. This is what you will see:
First thing you will need to do is to assign an environment property to the instance. Drag & drop our environment properties asset file to the Environment Properties field. Now we’ll have to setup the Ray Mask, which defines which layers will the raycasting be applied to. Set it to Everything for now.
Fire Transform determines where your shots are coming from. It is natural to think that this should be an object at the end of the weapon’s barrel etc. however in most cases this is your main camera. Because the Fire Transform will determine where your raycast is originating from, and in most cases you want to fire according to what your camera sees. Effects such as muzzle flashes, or your bullet time bullet’s starting position is determined separately, and that can be e.g an empty game object at the end of your weapon’s barrel. So let’s assign the Main Camera object to it in our empty scene.
Next thing is setting up Zero Distances. If you do not know about zeroing, I would definitely suggest going for a quick google search about it. Here the Zero Distances list is basically the available list of zero distances your game/scene should have. You setup this list once, and all other systems will pre-calculate their necessary simulation values according to this list. Then, you’d be able to cycle the current selected zero distance to setup your bullet’s zeroing, making it hit the target at the selected distance directly.
The system is hard-coded to have the first element of this list as 0. So this is how it would look like to setup a proper zero distance list. It is not recommended to go over 500 meters, in real-life most scopes can not adjust for such distances, and the simulation becomes less accurate the higher you go above 500 meters. This is mostly depending on your bullet’s muzzle velocity and air resistance as well. For instance, 1000 meters of zero distance on a 9mm bullet won’t be as accurate as it will be on a 7.62mm cartridge.Now let’s leave the Bullet Time disabled for now, and navigate to the Tracers section. When tracers are enabled, a tracer prefab will be spawned in each time you fire and it will follow the raycast path, simulating the effect of a bullet tracer.
Chance parameter defines what is the random chance of getting a tracer spawn on each time you fire, and Enable After parameter defines exactly how many seconds after firing the tracer will be enabled. Let’s leave them to default for now, but we need to assign a pooler instance that will be responsible for spawning the tracers. In the previous section Object Poolers, we have created an empty game object called Tracer Pooler, attached the pooler script on top of it & assigned the object to be pooled. Now let's drag and drop that object pooler instance to the Pooler field of tracer settings.
This is how our instance will look at this point. You can leave DrawDebugRays and DrawHitRays options enabled so that we can see the bullet’s path in Scene view. If you want an in-game debug, you can also enable it, setup an in-game debug pooler and use the TrajectoryRenderer prefab under Objects folder. Same logic as the tracers, but we will skip that for now. Since we’ve setup our SniperAndBallisticsSystem at the most basic level, we are now ready to fire a ballistics bullet!
Under InanEvin>Realistic Sniper and Ballistics>System Prefabs folder you can find a prefab called Sniper And Ballistics System. This prefab is a ready-to-use SniperAndBallisticsSystem.cs instance with a bunch of poolers, and hit listeners attached as a child. For now, we won't use it for this tutorial, but definitely keep it in mind that you can use it in your own scenes.