Raycast Barrel
The RaycastBarrel is a hitscan-based barrel implementation. It uses Unity’s Physics.Raycast to instantly detect and damage targets along a line.
Summary
This component:
- Inherits from
Barrel - Fires an invisible ray to simulate a bullet
- Detects
Hitboxcomponents and applies damage - Optionally spawns VFX at the point of impact
Fields
| Field | Type | Description |
|---|---|---|
_range | float | Max distance the ray travels. |
_layers | LayerMask | Layers that the raycast can interact with. |
_impacts | GameObject[] | Prefabs to spawn at impact (e.g., decals or sparks). |
hitEvents.OnHit | UnityEvent<Vector3, Vector3> | Invoked on successful hit with impact position and normal. |
Public Methods
| Method | Description |
|---|---|
override void Shoot() | Runs the base barrel logic, then FireRays(). |
void SpawnImpact(Vector3 point, Vector3 normal) | Instantiates visual effects at the hit location. Called automatically on hit. |
Protected Methods
| Method | Description |
|---|---|
virtual void FireRays() | Fires one ray per bullet from its spawn point. Override to change the pattern (the shotgun does). |
bool CastRay(Vector3 origin, Vector3 direction, out RaycastHit hit) | Casts one damaging ray: hurts any hitbox it reaches, raises OnHit and spawns impacts. |
Behavior
- Rolls damage per ray from the barrel’s
config.damage. - Searches for a
Hitboxon the collider hit by the ray. - If a hitbox is found, applies damage via
Hitbox.Hurt(DamageData). - Triggers
OnHitUnityEvent and optional impact prefabs.
Use Cases
- Rifles, pistols, machine guns
- Instant-fire weapons like lasers or tasers (visual-only)
- Sci-fi blasters without travel time
Tip
To prevent friendly fire, configure your LayerMask to only include enemy layers.
Note
You can chain custom logic to hitEvents.OnHit to spawn particles, sounds, or UI effects.