Recoil Component
The Recoil
component is responsible for simulating the visual and functional kickback of a weapon when fired. It can be used in two main ways:
- ๐ฏ Random recoil: Generates a light randomized movement in both X and Y axes
- ๐ฏ Spray pattern recoil: Follows a progressively randomized path between two animation curves
This component is highly versatile and can be used to create subtle kickback, chaotic recoil, or tight predictable spray patterns like those found in competitive shooters.
๐งฑ Hierarchy Setup
To use recoil properly in your weapon prefab, follow this structure:
flowchart TD A[Weapon Root] --> B(Recoil) B --> C(Weapon Model)
Note
The Recoil
object must be the second GameObject after the root, and all parts of the weapon should be children of it. The recoil system will apply positional and rotational offsets to this transform.
Note
Make sure that the tansform is centered to 0
๐ฎ How It Works
Each time a shot is fired:
- A new offset is calculated based on the recoil mode (random or spray)
- The recoil object moves accordingly
- Over time, the transform resets back to its neutral position using the Reset Speed
๐ Inspector Overview
Random Recoil Mode
This is the simplest configuration. The system will generate a random value between min
and max
values for both vertical and horizontal recoil.
Field | Description |
---|---|
Speed | How fast the recoil motion happens when firing |
Reset Speed | How quickly the weapon returns to its original position |
Recoil Vertical | Range of vertical kickback (Y axis) |
Recoil Horizontal | Range of horizontal drift (X axis) |
Spray Pattern Mode
If Recoil Vertical
and Recoil Horizontal
are set using curves, the system will follow a predefined spray path.
- Each axis uses two curves: a min and max range
- A random point between the two curves is chosen per shot
- The index along the curve increases with each shot to simulate escalating spray
Tip
This method gives you tight artistic control over recoil paths, useful for skill-based shooting mechanics.
๐ฌ In-Game Result
Hereโs an example of a spray pattern applied to a grenade rifle:
Each bullet hits along a progressively randomized path, creating a unique, learnable spread.
๐ Summary
- Place the recoil component on a child object above your weapon model
- Choose between random or spray by how you configure the curves
- Control sharpness with
Speed
and how quickly it stabilizes withReset Speed