Vehicle Weapons

The Weapons component provides key weapon functionality to a vehicle, and a vehicle should have this component added to the root transform.

Accessing Gun/Missile Weapons

The Weapons component gets all the weapons on the vehicle at the start as well as any loaded onto the vehicle during gameplay and stores them in the Gun Weapons and Missile Weapons lists. You can access them like so.

// Get the list of gun weapons
List<GunWeapon> gunWeapons = myWeapons.GunWeapons;

// Get the list of missile weapons
List<MissileWeapon> missileWeapons = myWeapons.MissileWeapons;

Selected Target

The Weapons Target Selector is the target selector that defines the target for gun lead target calculations and missile locking, and also defines the target that an AI will attack.

Set it in the inspector of the Weapons component.

You can access the selected target on the Weapons component like so:

// Get the selected target
Trackable selectedTarget = myWeapons.Target;

Turrets

The Weapons component accesses and stores all the turrets on the vehicle and aims them at the Turret Target Transform defined in the inspector.

With the Turrets Mode setting, you can define how turrets on the vehicle are handled:

  • Independent - the turret is left to its own devices to acquire targets and fire at them

  • Manual - the turret is aimed and fired by the vehicle

  • Auto - the turret target is provided by the Weapons Target Selector but it decides when to fire

Lead Target Positions

When a target is moving very fast across the line of sight, projectile weapons must be aimed ahead of it to hit it. Exactly where to aim is calculated as the lead target position. You can access the lead target information on the Weapons component like so:

// Get the lead target positions for each gun weapon in the Gun Weapons list
Vector3[] leadTargetPositions = myWeapons.LeadTargetPositions;

// Get the average lead target position, providing the target's actual position and velocity
Vector3 averageLeadTargetPosition = myWeapons.AverageLeadTargetPosition(targetPosition, targetVelocity);

Last updated