Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A simple RAIN{Indie} custom action for patrolling between waypoints
import RAIN.Action;
import RAIN.Core;
class AIPatrol extends RAIN.Action.Action
{
private var agentPosition : Vector3 = Vector3.zero;
private var targetLocation : Vector3 = Vector3.zero;
private var wayPath : RAIN.Path.RAINPathManager;
private var wayPathSize : int;
private var count = 0;
function newclass() {
actionName = "AIPatrol";
}
function Start(agent:Agent, deltaTime:float):ActionResult {
// setup the wayPath
if(wayPath == null){
wayPath = agent.PathManager;
targetLocation = getWaypointLocation();
}
//ensure waypoints are being used for patrolling
wayPath.useWaypointCollection = true;
wayPath.useNavigationGrid = false;
wayPathSize = wayPath.waypointCollection.transform.GetChildCount();
nextWaypoint();
return ActionResult.SUCCESS;
}
function Execute(agent:Agent, deltaTime:float):ActionResult {
agentPosition = agent.Avatar.transform.position;
var mag = (agentPosition - targetLocation).magnitude;
if(mag > 1.5){ // if not close enough to target
agent.MoveTo(targetLocation, deltaTime);
}
else {
nextWaypoint();
}
return ActionResult.RUNNING;
}
function Stop(agent:Agent, deltaTime:float):ActionResult {
return ActionResult.SUCCESS;
}
function nextWaypoint() {
// use the count to loop the array of waypoints
count++;
// last point goes back to the first point
if(count >= wayPathSize){
count = 0;
}
getWaypointLocation();
}
function getWaypointLocation() {
// one line way of getting the waypoints name (need to split as the name is 'waypoint123 (UnityEngine.Transform)')
currentPointName = wayPath.waypointCollection.transform.GetChild(count).ToString().Split(' '[0])[0];
targetLocation = wayPath.waypointCollection.transform.FindChild(currentPointName).transform.position;
return targetLocation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment