Demonstrates a code-driven set of general and custom agent behaviors for a unit.
Demonstrates a code-driven set of general and custom agent behaviors for a unit.
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.
Assets/Ride/Examples/Behaviours/AgentBehaviours/ExampleAgentBehaviours.unity
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.
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. |
Initialize the API prior to calling AgentBehaviour system.
Globals.api = ApiSystemMono.CreateApiSystemMono(false, true);
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 });
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
{
///
/// Base class for implementing agent behaviours
///
[System.Serializable]
abstract public class AgentBehaviour : RideBehaviour
{
///
/// The agent following this behaviour
///
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);
}
}
}