Game Development Community

Simple AI?

by NiN-NiN · in Torque 3D Beginner · 08/23/2013 (4:10 am) · 4 replies

Hi All

I'm running out of time for the game jam and I would normally search and try to work it out but i'm running out of time.

I was trying to look at the zombie AI as i need just an AI that is in an arena run to the player and melee attack like the zombies i've been looking to work out how to use it checking the zombie AI scripts but i'm having issues working out how to strip it to bare basics.

The AI doesn't have to avoid any objects it just needs to run to the player and play an animation to hit the player with it's club

Can someone point me in the right direction?


#1
08/23/2013 (6:15 am)
Every 500ms loop:
%this.setMoveDestination(%player.getPosition());

Make an AiPlayer onCollision() function to have it play an animation when it collides with a player - maybe use getClassName() and check that it is a "Player" class.

That's the quickest/dirtyest method.
#2
08/23/2013 (6:39 am)
Steve's way could look a bit like this:

// Bot move toward player example

function yourBot::onTargetEnterLOS(%this,%obj)
{
   // get info from target
   %count = ClientGroup.getCount();
   for(%i = 0; %i < %count; %i++)
   {
      %client = ClientGroup.getObject(%i);
      if (%client.player $= "" || %client.player == 0)
         return -1;
      %playerPos = %client.player.getPosition();
      %playerHealth = 1 - %client.player.getDamagePercent(); // health 0.0 - 1.0
   }
   
   // check if it's dead
   if (%client.player.getState() $= "Dead") // if (%playerHealth < 0.01)
   {
      %obj.clearAim();
      %obj.setMoveSpeed(0.5);  // decrease speed
   }
   else
   {
       // do this if its the beginning of the attack         
       %obj.setAimObject(%client.player, "0 0 1"); // aim at player
       %obj.setMoveSpeed(1); // increase the speed
       %obj.setMoveDestination(%playerPos); // move towards player
   }
}

Of course if you have zombies you could remove the check if the player is dead.
#3
08/23/2013 (6:48 am)
Thanks guys :)

I now have running and attacking working and now i can kill the monster too ;)

i7.photobucket.com/albums/y279/aussie_ninja/Torque%203D/monster_zps13b3bad9.pngi7.photobucket.com/albums/y279/aussie_ninja/Torque%203D/monster2_zpsb00ae817.png
#4
08/23/2013 (6:57 am)
For the collision you could have something like this:

// Example!!!

function yourBot::onCollision(%this, %obj, %col)  
{  
   if(isObject(%obj)) 
   {  
      if (%col.getClassName() $= "Player")
      {
         %botDamage = %obj.getDamagePercent(); // damage 0.0 - 1.0
         %playerHealth = 1 - %col.getDamagePercent(); // health 0.0 - 1.0
         if (%botDamage < 1)
         {
            if (%playerHealth > 0)
            {     
 
               %obj.playThread(0, "Attack"); // play the "Attack" animation
               serverPlay3D(PainCrySound, %obj.getTransform()); // player cries out
               %col.applyDamage(20); // Set the amount of damage
               %obj.ailoop = %obj.schedule(10, "Think", %obj); // bot, wait a moment

            }
            else
            {
               %obj.clearAim();
               %obj.setMoveSpeed(0.5);
            }
         }
      }
   }  
}

function yourBot::onAdd(%this,%obj)
{
   %obj.setMoveSpeed(0.5);
}