I use the term ‘core’ to represent the main high-level manager for the object. Think of this as the heart-beat or life-cycle. In this case, the Gun Core is responsible for firing bullets, playing sounds, and determining accuracy.
Along with the Gun Core, we use “item components” to augment the capabilities of the gun. For example, the mullet component defines the decal to use for bullet holes while the muzzle component defines the sound effects to use when firing.
To learn more about the gun components, click here.
Local Position and Rotation
When the gun is instantiated, we use the Local Position and Local Rotation properties to position the gun in the character’s right hand.
Since the gun is attached to the character’s right hand, these local parameters are relative to the character’s skeleton. What that means is that the values I use in the “M4_Mixamo” prefab may not be the right local position and rotation for your character. It just depends on how the character’s skeleton is built.
You may find that you need to create an “M4_Morph3D” or other prefab if you’re using a different character skeleton. In this case, everything is probably the same except for the local position and local rotation values. These will need to fit your character.
Finding the Local Values
Finding the local position and local rotation values for your character is pretty easy. You simply run Unity in play mode and get your character to enter the gun idle or gun firing animation like you see above. Then press ‘pause’.
Add the gun to your scene (if it’s not there already). It will probably be in some odd position and rotation.
Use the standard Unity move and rotation tools to put the gun in to the character’s right hand. Make sure the barrel is facing forward and parallel to the ground.
Copy the transform information of the gun. This is what we’re going to set in our Gun Core.
Finally, we stop the Unity and head back to the editor. On our gun prefab, we enter these values in the Gun Core.
The next time we run and equip the gun, it will use these local properties.
While this video is about setting the local position and rotation for a sword, the process is the same.
The muzzle transform is the transform that represents the position and rotation of the gun’s muzzle. It’s where we’ll fire bullets from, play effects from, etc.
We just need to make sure your gun prefab has this transform and that we set the transform in the Muzzle Property of the Gun Core.
Recoil and Spread
To learn more about recoil and spread, check out the page on Shooting.
The recoil values determine the horizontal and vertical movement of the gun after each shot.
The spread values represent the minimum and maximum diameter where the bullet lands at max range. The bullet will impact a random location within that circle.
At the bottom of the Gun Core, we can check the “Show Debug Info” to render debug data to the screen at run-time. This is useful to make sure our aim, spread, and firing is correct.
In order for this to work, we also need to ensure the “Main Camera” has our “Graphics Manager” component. This is what will do the actual debug rendering.