Laser Guide
The LaserGuide script allows projectiles spawned by a ProjectileBarrel to dynamically follow a target using laser guidance. Players can toggle the laser system on and off at runtime, affecting all active projectiles.
Summary
This component:
- Listens for
LaserFollowerprojectiles - Assigns a dynamic
targetto them if laser is active - Allows toggling laser visibility and tracking behavior via input
- Cleans up inactive projectiles on impact
Fields
| Field | Type | Description |
|---|---|---|
target | Transform | The transform that projectiles will follow when laser is active. |
barrel | ProjectileBarrel | The source barrel responsible for spawning the projectiles. |
laser | GameObject | The laser pointer object shown/hidden on toggle. |
Private Fields
| Field | Type | Description |
|---|---|---|
controls | WeaponControls | Input mapping for toggling laser guidance. |
laserActive | bool | Whether laser guidance is currently enabled. |
_activeProjectiles | List<LaserFollower> | List of currently active guided projectiles. |
Methods
| Method | Description |
|---|---|
Start() | Subscribes to projectile spawn and input events. |
ToggleLaser() | Toggles laser tracking and updates all active projectiles accordingly. |
Behavior
- When a projectile with
LaserFolloweris spawned, itstargetis set based onlaserActive. - If laser is disabled mid-flight, projectiles will stop tracking.
- Projectiles are removed from the list once their
OnImpactevent triggers. - The system listens to
ShootSecondaryinput to toggle.
Requirements
- Projectiles must have a
LaserFollowercomponent. ProjectileBarrelmust invokeOnProjectileSpawned.
Use Case Examples
- Laser-guided rockets
- Smart missiles
- Tactical pointers for enemy tracking
Tip
Use SetActive(false) on the laser GameObject to make the laser visually disappear when guidance is disabled.
Note
This component works best with LaserFollower-based projectiles that interpret target.