Game Development Community

How to make bots follow a player. A centipede.

by Robert Brower · 11/09/2004 (7:23 am) · 8 comments

I used TGE 1.3 following these steps...

1. In starter.fps/server/scripts/aiplayer.cs, change function DemoPlayer::onReachDestination so that it just returns. You could probably just comment it out in it's entirety.

function DemoPlayer::onReachDestination(%this,%obj)
{
return;
}

2. Add the following script at the bottom of the same file as above, starter.fps/server/scripts/aiplayer.cs.

function AIPlayer::followPlayer(%this,%player)
{
%this.stopThread(0);
if (!isObject(%player)) {
%this.player = "";
return;
}
%this.following = %player;
%this.think();
}

function AIPlayer::think(%this)
{
%player = %this.following;
%y = getWord(%player.getDataBlock().boundingBox, 1);
%pos = getWords(%player.getTransform(), 0, 2);
%oldPos = %pos;
%vec = " 0 -1 0";
%vec = MatrixMulVector(%player.getTransform(), %vec);
%pos = "0 0 0";
%pos = VectorAdd(%oldPos, VectorScale(%vec, %y));

%followPos = %pos;
%pos = getWords(%this.getTransform(), 0, 2);
%vec = VectorSub(%followPos, %pos);
%len = VectorLen(%vec);

%this.setAimObject(%player);
if (%len > getWord(%this.boundingBox, 1))
%this.setMoveDestination(%followPos, false);
%this.schedule(30,think);
}

3. In starter.fps/server/scripts/health.cs, change function HealthPatch::onCollision so that it looks like this:

function HealthPatch::onCollision(%this,%obj,%col)
{
// Apply health to colliding object if it needs it.
// Works for all shapebase objects.
if (%col.getState() !$= "Dead" ) {
%col.applyRepair(%this.repairAmount);
%obj.respawn();
if (%col.client)
messageClient(%col.client, 'MsgHealthPatchUsed', '\c2Health Patch Applied');
}

// Create the player object
%aiplayer = new AIPlayer() {
dataBlock = DemoPlayer;
};
MissionCleanup.add(%aiplayer);

// get the last follower
%player = %col;
while (isObject(%player)) {
if (isObject(%player.follower))
%player = %player.follower;
else break;
}

%player.follower = %aiplayer;
%y = getWord(%player.getDataBlock().boundingBox, 1);
%pos = getWords(%player.getTransform(), 0, 2);
%oldPos = %pos;
%vec = " 0 -1 0";
%vec = MatrixMulVector(%player.getTransform(), %vec);
%pos = "0 0 0";
%pos = VectorAdd(%oldPos, VectorScale(%vec, %y));

%aiplayer.setTransform(%pos);
%aiplayer.setMoveSpeed(1.0);
%aiplayer.followPlayer(%player);
}

Add a bunch of healthPatches to a mission file then go into the game and pick them all up. You can see the bots following their leader. Enjoy.

Robert Brower

#1
11/09/2004 (5:17 pm)
ooh, thats cool Robert, you should do a centipade game now, if you have time...
#2
11/17/2004 (3:58 am)
this is something i was thinking about,
any chance of being able to code in stuff like
"stay" and it'll stay,and "follow me" and it will follow?
#3
11/17/2004 (5:35 am)
Sure it's possible. Easily scripted.
#4
11/17/2004 (8:53 am)
how would we go about doing that,
i need to setup a friendly AI character
to "stay" and "follow"
a little bit of info as what scripts
are involved would be of immense help
#5
12/07/2004 (7:19 pm)
Jojimbo, the point of this resource is to show you how to do what it is you're asking. At least, the following part.

Robert
#6
07/16/2007 (12:46 am)
couldnt u just put in a code that just resets the "followers" to not follow if u press a certain comand
and if u press another command it just comes to point directed and then follows again
#7
06/17/2008 (1:56 am)
Do you think this could be modified so I could implement a trailer that follows my truck without lag?
#8
08/09/2008 (5:32 am)
Awesomeness...