Recoil
The Recoil component handles procedural recoil animations using rotation. It reacts to each shot and resets over time, allowing natural, dynamic aiming motion.
Summary
This component:
- Applies local rotation to simulate vertical and horizontal recoil
- Uses animation curves (
MinMaxCurve) to vary recoil patterns - Smoothly returns to default position using
Lerp
Fields
| Field | Type | Description |
|---|---|---|
_barrel | Barrel | Barrel whose shots kick this recoil. Leave empty to follow every barrel under this object. |
speed | float | How quickly recoil progresses per shot. |
resetSpeed | float | How quickly the recoil resets to neutral. |
recoilVertical | MinMaxCurve | Vertical (pitch) rotation applied per shot. |
recoilHorizontal | MinMaxCurve | Horizontal (yaw) rotation applied per shot. |
Public Methods
| Method | Description |
|---|---|
void Evaluate(float fireRate) | Applies new recoil based on fire rate and curve values. |
void Reset() | Resets recoil step so it starts fresh next time. |
Internal Behavior
- On enable, the recoil subscribes to
events.OnShotandevents.OnEndShootingof its barrel (or of every barrel under it), and unsubscribes on disable. Evaluate()is called on eachOnShot, andReset()on eachOnEndShooting. The weapon and barrel hold no reference to the recoil.- Rotation is applied using
Quaternion.Euler(), scaled using fire rate and noise. Update()smooths rotation back to default every frame.
Example Use
Recoil works on its own once placed. To kick it manually (for example from a custom barrel that doesn’t raise OnShot):
recoil.Evaluate(barrel.FireRate);Note
Recoil is applied to the GameObject this script is attached to. You can make it a pivot child of the weapon model to isolate >motion.
Tip
You can use MinMaxCurve to randomize or control recoil escalation with time or shot count.
See Also Weapon