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 Hitbox components and applies damage
  • Optionally spawns VFX at the point of impact

Fields

FieldTypeDescription
_rangefloatMax distance the ray travels.
_layersLayerMaskLayers that the raycast can interact with.
_impactsGameObject[]Prefabs to spawn at impact (e.g., decals or sparks).
hitEvents.OnHitUnityEvent<Vector3, Vector3>Invoked on successful hit with impact position and normal.

Public Methods

MethodDescription
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

MethodDescription
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 Hitbox on the collider hit by the ray.
  • If a hitbox is found, applies damage via Hitbox.Hurt(DamageData).
  • Triggers OnHit UnityEvent 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.

See Also