To give you the greatest flexibility with designing your game, the player and AI are separated from the vehicles that they control, enabling them to switch vehicles seamlessly during gameplay.
To create a new player or AI, simply create a new game object and add a GameAgent component.
To make the Game Agent a player, simply check the 'Is Player' checkbox in the inspector of the GameAgent component.
To make the Game Agent an AI/NPC, simply uncheck the 'Is Player' checkbox in the inspector of the GameAgent component.
To make the Game Agent enter a vehicle when the scene starts, simply drag a vehicle (not a prefab, must be a vehicle in the scene) into the 'Starting Vehicle' field in the inspector of the GameAgent component.
// Enter a vehicle
// Exit vehicle
Game Agents are automatically killed when the vehicle they are in is destroyed.
Here are examples for how to use the relevant functions:
// Kill the game agent
// Revive the game agent
// Run some code if the game agent is dead
// Some code
To make it as easy as possible to create a scene for your game, the GameAgentManager component has been provided which does several key things:
- Stores all the Game Agents in the scene for easy access
- Stores a reference to the 'Focused Game Agent' which provides a single reference point to the player for things like camera following, rumble effects, etc (it can be an AI too, if desired).
- Provides events for other systems to receive notifications when the player switches vehicles, dies, is revived, etc
The GameAgentManager is a singleton class, which means there must only be one in the scene at any time.
All you need to do is add the component anywhere in the scene and other scripts can find it using the static GameAgentManager.Instance reference, for example to access the Game Agents in the scene:
// Get a list of all the game agents in the scene
List<GameAgent> gameAgentsInScene = GameAgentManager.Instance.GameAgents;