Game Agents

A Game Agent component represents a player or AI, and is separate from any vehicle so that it can enter and exit different vehicles during gameplay.

A Game Agent can take control of different vehicles, be killed or revived, and belong to a Team.

Learn how to create a Player or an AI here.

Check out the inspector for all the settings you can customize. Here are some of the key things to know about this component.

Setting The Starting Vehicle

To make the Game Agent enter a vehicle when the scene starts, drag a vehicle (not a prefab, must be a vehicle in the scene) into the 'Starting Vehicle' field in the inspector.

Getting The Current Vehicle

To get the vehicle that a Game Agent is currently in, use the following code:

// Get the vehicle that the Game Agent is in
Vehicle vehicle = someGameAgent.Vehicle;

Entering/Exiting Vehicles During Gameplay

To enter/exit a vehicle during gameplay, simply call the Game Agent's Enter Vehicle function like so:

// Enter a vehicle
myGameAgent.EnterVehicle(myVehicle);

// Exit vehicle
myGameAgent.EnterVehicle(null);

If you have a character and you want it to be able to enter/exit a vehicle when it's nearby, check out the enter/exit system.

Killing/Reviving Game Agents

Game Agents are automatically killed when the vehicle they are in is destroyed.

Here are examples for how to use the relevant functions manually.

// Kill the game agent
myGameAgent.Kill();

// Revive the game agent
myGameAgent.Revive();

// Check if the game agent is dead
if (myGameAgent.IsDead)
{
    // Some code
}

Accessing Game Agents

To make it as easy as possible to access all the game agents in the scene, the Game Agent Manager component has been provided.

To set up the Game Agent Manager, just add this component anywhere in the scene.

The Game Agent Manager is a singleton class, which means there must only be one in the scene at any time.

This component does several key things:

  • Stores all the Game Agents in the scene for easy access

  • Stores a reference to the 'Focused Game Agent' (the player) which provides a single reference point to the player for things like camera following, rumble effects, etc.

  • Provides events for other systems to receive notifications when the player switches vehicles, dies, is revived, etc

Any script can access the Game Agent Manager using the static 'GameAgentManager.Instance' reference:

// Get a reference to the player
GameAgent player = GameAgentManager.Instance.FocusedGameAgent;

// Get a list of all the game agents (player or AI) in the scene
List<GameAgent> gameAgentsInScene = GameAgentManager.Instance.GameAgents;

Last updated