ENEMY AI HELP

[Arch] DeutchesKaiserreich
Joined 31/10/2021
Posts 96
02:37 PM 18/03/2023
I am making a game called Crossfire it is simmilar to trench games but instead im using Enemy AI to attack the trench but I do not know on how to make one. Perhaps somebody can tell me? Thanks.


havoq
Joined 05/01/2022
Posts 11
02:19 PM 28/03/2023
It's not really an "AI" but I figured this might help.


let damage = 10; //Amount of damage you want the enemy to deal

let outfit = new Outfit(); //An object to set the appearance of a bot
outfit.body("#FF0000");
outfit.rightLeg("#FFFF00");
outfit.leftLeg("#FFFF00");

let enemy = new Bot("Enemy"); //Creating the Bot object
enemy.setOutfit(outfit);
Game.newBot(enemy); //Adding the bot into the world

enemy.setInterval(() => { //This is self explanatory
let player = enemy.findClosestPlayer(70); //Find the closest player with a radius of 70 studs

if (!player) {return;}

enemy.moveTowardsPlayer(player, 12);

enemy.touching((plr) => {
plr.setHealth(plr.health - damage);
});

}, 5000); //Do this every 5000 milliseconds (5 seconds)

This is slightly based of a part of the code I'm using to create my game. I haven't teste it out yet, so let me know if it works.


[RWB] MixaMega
Joined 23/12/2019
Posts 435
Moderator
07:10 PM 28/03/2023
Quote from havoq , 02:19 PM 28/03/2023
It's not really an "AI" but I figured this might help.


let damage = 10; //Amount of damage you want the enemy to deal

let outfit = new Outfit(); //An object to set the appearance of a bot
outfit.body("#FF0000");
outfit.rightLeg("#FFFF00");
outfit.leftLeg("#FFFF00");

let enemy = new Bot("Enemy"); //Creating the Bot object
enemy.setOutfit(outfit);
Game.newBot(enemy); //Adding the bot into the world

enemy.setInterval(() => { //This is self explanatory
let player = enemy.findClosestPlayer(70); //Find the closest player with a radius of 70 studs

if (!player) {return;}

enemy.moveTowardsPlayer(player, 12);

enemy.touching((plr) => {
plr.setHealth(plr.health - damage);
});

}, 5000); //Do this every 5000 milliseconds (5 seconds)

This is slightly based of a part of the code I'm using to create my game. I haven't teste it out yet, so let me know if it works.
you dont have to make the outfit like that, the outfit functions return the outfit object, so you can chain them

let outfit = new Outfit()
.body("#FF0000");
outfit.rightLeg("#FFFF00");
outfit.leftLeg("#FFFF00");


[RWB] MixaMega
Joined 23/12/2019
Posts 435
Moderator
07:11 PM 28/03/2023
i pressed something wrong, anyway here

let outfit = new Outfit()
.body("#FF0000")
.rightLeg("#FFFF00")
.leftLeg("#FFFF00");


havoq
Joined 05/01/2022
Posts 11
12:45 PM 29/03/2023
Quote from MixaMega , 07:10 PM 28/03/2023
Quote from havoq , 02:19 PM 28/03/2023
It's not really an "AI" but I figured this might help.


let damage = 10; //Amount of damage you want the enemy to deal

let outfit = new Outfit(); //An object to set the appearance of a bot
outfit.body("#FF0000");
outfit.rightLeg("#FFFF00");
outfit.leftLeg("#FFFF00");

let enemy = new Bot("Enemy"); //Creating the Bot object
enemy.setOutfit(outfit);
Game.newBot(enemy); //Adding the bot into the world

enemy.setInterval(() => { //This is self explanatory
let player = enemy.findClosestPlayer(70); //Find the closest player with a radius of 70 studs

if (!player) {return;}

enemy.moveTowardsPlayer(player, 12);

enemy.touching((plr) => {
plr.setHealth(plr.health - damage);
});

}, 5000); //Do this every 5000 milliseconds (5 seconds)

This is slightly based of a part of the code I'm using to create my game. I haven't teste it out yet, so let me know if it works.

you dont have to make the outfit like that, the outfit functions return the outfit object, so you can chain them

let outfit = new Outfit()
.body("#FF0000");
outfit.rightLeg("#FFFF00");
outfit.leftLeg("#FFFF00");
Thanks for this, I didn't know that those functions returned the object itself.

1