Incorporating Player
Last updated
Last updated
So far, the information we have learned should be enough for you to easily start using RSB in your own player controllers & character systems. However, if you don't have your own character system, or if you want to build upon a base, you can use the example first-person controller provided by RSB. Let's change the tutorial scene such that instead of firing from the main camera, we will fire from the player camera.
First, delete the Main Camera object. Then navigate to InanEvin>Realistic Sniper and Ballistics>System Prefabs folder and drag & drop the Player Without Weapon prefab into the scene. Position it to 0,1,0. Then go over our Tutorial object, you will see that the Main Camera is missing. Drag & drop the Main Camera under the player prefab in the hierarchy. Then go to the Sniper and Ballistics object and assign the same Main Camera to the Fire Transform field of the SniperAndBallisticsSystem.cs instance. Next, go over to the ScopeReticle object under CANVAS - Dynamic Scope System where the DynamicScopeSystem.cs component is attached, and assign the same Main Camera under the player to the Main Camera field. Finally, open up our Tutorial.cs script, and modify the if block where we are firing to:
Remember we used to fire using Space key, but that will conflict with the Jump input of the player controller we just dragged in. Now, we have just switched that to the left-click mouse button.
There is only a small thing left to do. The Player without Weapon prefab, as well as the other player prefab we will talk about in the upcoming sessions, are layered as IgnoreRaycast. This is only for the sake of the demo scenes, of course in your own project, you might want to raycast to the player, e.g. from an enemy, so you might want to put the player into some new layer like Player. But for the RSB package, we did not want to mess with your project settings & layers, so we have just put the player under IgnoreRaycast. Now, it is possible that when we fire from the player, the rays might hit any object under the player, if we were to have an object with a collider. That's why we want to bypass the player during our raycasts. Go to the Sniper and Ballistics object, and change the Ray Mask value to Everything but IgnoreRaycast (Mixed).
Since the player is in IgnoreRaycast layer, now the rays casted from our fire transform has no chance of hitting the player collider, or any object we put under the player if there were to be one.
If you play your scene now, you will be able to walk around, and shoot with the mouse left-click. Remember, you can also aim with right-click.
Let's also debug the bullet trajectory in-game. Create a new game object, call it Trajectory Renderer Pooler, attach the ObjectPooler.cs component on it. Then click on the search field of the Pooled Object field and assign the Bullet Trajectory Renderer prefab on it.
Then go over to the Sniper and Ballistics object, enable In-game Debug renderer settings, and assign this pooler to the Trajectory Renderer Pooler field.
Now if you play the game and shoot, you will see the bullet's trajectory being rendered with line renderers.
Let's take a look at the details of the player prefab we have just dragged in, just to understand how it works.