Skip to content

Instantly share code, notes, and snippets.

@ellerynz
Created October 27, 2013 23:30
Show Gist options
  • Save ellerynz/7189202 to your computer and use it in GitHub Desktop.
Save ellerynz/7189202 to your computer and use it in GitHub Desktop.
A RAIN{Indie} custom action to chase a cylinder as long as it is in sight.
import RAIN.Action;
import RAIN.Core;
class AIMoveToCylinder extends RAIN.Action.Action
{
private var wayPath : RAIN.Path.RAINPathManager;
private var raySensor : RAIN.Sensors.RaycastSensor;
private var cylinder: GameObject;
function newclass() {
actionName = "AIMoveToCylinder";
}
function Start(agent:Agent, deltaTime:float):ActionResult {
// get the cylinder based on the variable name in the detect action
cylinder = agent.actionContext.GetContextItem.<GameObject>("CylinderPosition");
// get the sensor
raySensor = agent.GetSensor("Sensor");
// setup the wayPath
if(wayPath == null){
wayPath = agent.PathManager;
}
// ensure navgrid is being used for chasing
wayPath.useWaypointCollection = false;
wayPath.useNavigationGrid = true;
return ActionResult.SUCCESS;
}
function Execute(agent:Agent, deltaTime:float):ActionResult {
// if the AI can see the cylinder
if (cylinder != null && raySensor.CanSee(cylinder)) {
if (!agent.MoveTo(cylinder.transform.position, deltaTime)) {
return ActionResult.RUNNING;
}
}
else {
// keep moving to where the cylinder was
agent.actionContext.SetContextItem("canSeeCylinder", 0);
if (!agent.MoveTo(agent.PathManager.moveTarget.VectorTarget, deltaTime)) {
return ActionResult.RUNNING;
}
}
return ActionResult.SUCCESS;
}
function Stop(agent:Agent, deltaTime:float):ActionResult {
return ActionResult.SUCCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment