class CustomAIController extends AIController;
var float speedMod;
var float chaseDistance;
var float distanceToPlayer;
// 2 lines below is essencial for pathfinding
var Actor target;
var Vector selfToPlayer;
var float MonsterChasingPlayer;
var CustomPawn playerPawn;
auto state Idle
{
event SeePlayer(Pawn seePawn)
{
if (playerPawn == none)
playerPawn = CustomPawn(seePawn);
distanceToPlayer = VSize(self.pawn.location - playerPawn.Location);
if (distanceToPlayer < chaseDistance)
{
self.pawn.SetPhysics(PHYS_WALKING);
GotoState(\'Chase\');
}
}
}
state Chase
{
event Tick(float deltaTime)
{
local Rotator DeltaRot;
selfToPlayer = playerPawn.Location - self.Pawn.Location; // calculate the offset between the player and the bot, in X Y Z coordinates. Direction of vector matters here.
distanceToPlayer = VSize(selfToPlayer); // calculate the distance between the bot and the player, based on the previously calculated offset vector "selfToPlayer".
// this is done by calculating the length of the offset vector.
DeltaRot = rotator(selfToPlayer); // create a rotator that represents the rotation required to face towards the player.
DeltaRot.Pitch = 0; // remove any rotation that would cause the bot to move up into the air.
if (distanceToPlayer < chaseDistance)
{
self.Pawn.Velocity = Normal(selfToPlayer) * self.pawn.GroundSpeed * deltaTime; // set the velocity (speed) of the bot.
// velocity = normalize the offset vector "selfToPlayer" and multiply it by an arbitrary value,
// which will increase the speed.
self.Pawn.FaceRotation(RInterpTo(DeltaRot, rotator(selfToPlayer), deltaTime, 60000, true), deltaTime); // actually rotate the bot.
self.Pawn.Move(self.Pawn.Velocity); // actually move the bot according to the velocity.
}
else
{
self.pawn.SetPhysics(PHYS_NONE);
GotoState(\'Idle\');
}
}
event Touch(Actor Other, PrimitiveComponent OtherComp, Object.Vector HitLocation, Object.Vector HitNormal)
{
}
}
DefaultProperties
{
chaseDistance = 700
}