ProjectileAmmo
The ProjectileAmmo script represents a physical bullet with a Rigidbody. It moves through space and applies damage on collision.
Summary
This component:
- Launches a projectile with velocity
- Detects collisions and applies damage
- Supports impact VFX and time-based destruction
Fields
| Field | Type | Description |
|---|---|---|
_speed | float | Speed of the projectile (default: 15). |
_lifetime | float | How long the projectile lives before self-destruction. |
OnImpact | UnityEvent<DamageData> | Invoked when the projectile hits something. |
Public Methods
| Method | Description |
|---|---|
void SetSource(Transform spawnPoint) | Sets the initial position and rotation of the projectile. Should be called before shooting. |
void Shoot(int damage, GameObject owner) | Launches the projectile in the forward direction with force. |
void CreateImpact(GameObject prefab) | Instantiates a VFX or decal prefab at the hit position. |
Collision Behavior
- On collision:
- Creates a
DamageDatainstance - Applies damage to
Hitboxif found - Invokes
OnImpact - Destroys the projectile
- Creates a
Note
Assign a Rigidbody and Collider to the projectile prefab.
Tip
Use pooling instead of Destroy() for optimization in high-fire-rate weapons.