Game Development Community

AI walking towards me

by Fontys (#0011) · in Technical Issues · 06/16/2007 (12:41 pm) · 7 replies

Hi,

In game i'm having some bots to run towards me, and when they hit me, i get damaged. They get the location i'm at, run towards there, and when the OnReachDestination functions hits, they do run towards me again...

This doesnt work that smooth, because they will always be kinda late.

Does anyone have any suggestions how i can make this smoother?

This is my code:

function MyBot::onReachDestination( %this, %obj )
{
echo("\n\nInside MyBot::onReachDestination\n\n");

//%obj.gotoRandomPoint(%this) ;
//%obj.setMoveDestination(localclientconnection.player.getPosition());
%this.GotoPlayer( %obj );
}

function MyBot::GotoPlayer( %this, %obj )
{
echo("into GoToPlayer");
echo("player object" SPC %this );
%obj.setMoveDestination(localclientconnection.player.getPosition());
//%this.schedule( 1000, GotoPlayer );
}

The schedule doesnt work :(....so they'll keep waiting for the 'onReachDestination'.

Thx in advance!!!

About the author

Recent Threads


#1
06/16/2007 (1:18 pm)
OnReachDestination is called by the datablock not the object

eg:
function createBot()
{
   new AIPlayer(MyBot)
   {
      datablock = MyBotDataBlock;
   }
}

function MyBotDataBlock::onReachDestination( %this, %obj )
{
   // %this - points to the datablock itself
   // %obj - points to the object the datablock is part of:  Eg MyBot
 
   %obj.setMoveDestination( localclientconnection.player.getPosition() );
}
#2
06/16/2007 (7:11 pm)
Away from the script side of things...

If your player will be moving about you will likely be wanting to update the location of the destination for the bots. By this I mean: say your player is at point X, in 3 seconds time he might be at point Y, this means your bots will be going to point X still (no longer the player location)

Within some sort of think function for your bots make it so that the bot's destination is always the player's location. I'm not sure which would be quicker tho, to just constantly update their destination or bother checking to see if the player has changed location (chances are the player will change location ALL the time... so scrap my last thought and just update their destination position every think function :D)
#3
06/16/2007 (9:58 pm)
If you want to make things a bit more intelligent, try using a pursuit technique instead of a seek technique. Pursuit aims the chasing AI at a point the player should reach in the future (much as we do when we are chasing someone) instead of the position they are currently in.

There are some fairly easy examples available based on the current positions of the AI and the player and their current velocities. As Craig mentioned you will want to update the future point location the AI will aim at every so often. This should smooth your AI player's movement out.


K
#4
06/19/2007 (11:34 am)
Thx for the help!!

A pursuing technique would be awesame, but a bit too complicated!

To speed-up the update for the setMovement function would be great and sounds a bit easier. I cant find a function that covers this, i guess it would be something like PlayerShape::onMove?

And how do I iterate through my bots? I read something that this should be done with a datablock?
#5
06/19/2007 (1:53 pm)
Here is one of many ways you could do this:

Put this at the top of your "AIPlayer.cs".
new SimSet(AIGroup);
Then make sure you have a functions like,
function AIPlayerDataBlock::onAdd(%this, %obj)
{
   AIGroup.add(%obj);
}
Next put this in "player.cs".
function Player::startAIChase(%this)
{
   %totalAIs = AIGroup.getCount();
   %centerPos = %this.getWorldBoxCenter();
   InitContainerRadiusSearch(%centerPos, 50, $TypeMasks::PlayerObjectType);

   for (%i = 0; %i < %totalAIs; %i++)
   {
      %player = ContainerSearchNext();
      if (isObject(%player) && %player.getClassName() !$= "Player")
         %player.setMoveDestination(%this.getPosition());
   }

   %this.search = %this.schedule(500, "startAIChase");
}
Last, put this inside your players "onAdd" fucntion,
%obj.startAIChase();
and inside "onRemove",
cancel(%this.search);

I hope this is what your looking for.
#6
06/19/2007 (3:14 pm)
Thx!

that works perfect! :D
#7
06/19/2007 (3:15 pm)
Fontys what you have there is probably suitable for what you're doing. the reason the schedule isnt working is because you must also pass in the parameters you want the scheduled function to be called with:

%this.schedule( 1000, GotoPlayer );

this should be:

%this.schedule( 1000, GotoPlayer, %obj);

since GotoPlayer is a datablock function, the implicit %this variable will get passed automatically, but you must still specify the %obj parameter for the next time the function is called.