Purpose

Demonstrates a code-driven set of general and custom agent behaviors for a unit.

How to Use

At run-time, use the arrow buttons at the top to cycle through the available BLUFOR agent behaviours in the example scene.

The RIDE API allows behaviours to be set on individual and groups of agents.  The behaviours define how the agent will act when updated each frame. 

Scene Location & Name

Assets/Ride/Examples/Behaviours/AgentBehaviours/ExampleAgentBehaviours.unity

Setup Requirements 

Utilize agent behaviours by adding the ExampleAgentBehaviours script, and in the case of the example scene, cover objects and waypoint prefabs. 

Both the cover and waypoint objects must be added as entries in the Cover Objects and Path fields of the Example Agent Behaviours script.

Use the scene-ready Waypoint_AutoAddToRide prefab.

For the cover objects, ensure each has Nav Mesh Obstacle and Entity Mono script components, with Attribute set on the latter to “cover” option.

ExampleAgentBehaviours Script

Add this script to an object in your scene which at run-time will perform the following as-is: 1) spawn BLUFOR unit, 2) spawn OPFOR unit, and 3) make available a predefined set of four behaviours for the BLUFOR unit w/debug menu button toggle.

Note

The below information pertains to advance use of the AgentBehaviour system.

Initialization

Initialize the API prior to calling AgentBehaviour system.

				
					
Globals.api = ApiSystemMono.CreateApiSystemMono(false, true);
				
			

Agent System

Example of an agent with “wander” behaviour.

				
					
Unit unit = new Unit()
{
    team = Team.Blue,
    name = "Adam",
    prefab = "ChrUsaArmyInfantryAcu01Prefab",
    health = 100,
    maxHealth = 100,
    items = new ItemType[] { ItemType.m4 },
    pos = Position.zero,
    rot = Quaternion.identity,
};

// create agent and set its speed
Rideid agent = Globals.api.scenarioSystem.AddAgent(unit);
Globals.api.agentSystem.SetAgentSpeed(agent, 1, 2);

// set behaviour
Globals.api.agentSystem.SetAgentBehaviour(agent, new Wander() { minSecondsToWait = 2f, maxSecondsToWait = 4f, wanderRadius = 4 });
				
			

Creating Behaviours

You can create your own behaviours by inheriting from the AgentBehaviour class (or IAgentBehaviour interface if you want to do more work yourself).

AgentBehaviour.cs

				
					
namespace Ride.Behaviour
{
    /// <summary>
    /// Base class for implementing agent behaviours
    /// </summary>
    [System.Serializable]
    abstract public class AgentBehaviour : RideBehaviour
    {
        /// <summary>
        /// The agent following this behaviour
        /// </summary>
        protected Rideid agent { get { return this.entity; } }

        public AgentBehaviour() { }
        public AgentBehaviour(IApi api, Rideid agent) { Init(api, agent); }

        protected override string GetEntityName()
        {
            return api.scenarioSystem.GetEntityName(entity);
        }
    }
}