Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
Melee

Kela
Guest
Quote
2022-10-17 07:49:36

How do I make melee enemy that follows you using javascript


okeoke
Guest
Quote
2022-10-18 00:09:45

Hi Kela,

The most basic implementation is probably something like the following:
//get player position:
var plNode = ccbGetSceneNodeFromName('PLAYER_NODE_NAME');
var plPos = ccbGetSceneNodeProperty(plNode, 'Position');

// use ai command to move to a player position
ccbAICommand(node, 'moveto', plPos);

Check distance and if it's close enough attack player

var enPos = ccbGetSceneNodeProperty(node, 'Position');
if (enPos.substract(plPos).getLength() < 50) {
ccbAICommand(node, 'cancel');
ccbAICommand(node, 'attack', plNode);
}

I guess, you also need to give your enemy a cool down. You can just assign two properties:
this.curCd = 0;
this.cd = 3000;

and add an if statement like:


if (this.curCd <= 0) {
var plNode = ccbGetSceneNodeFromName('PLAYER_NODE_NAME');
var plPos = ccbGetSceneNodeProperty(plNode, 'Position');

ccbAICommand(node, 'moveto', plPos);

var enPos = ccbGetSceneNodeProperty(node, 'Position');
if (enPos.substract(plPos).getLength() < 50) {
ccbAICommand(node, 'cancel');
ccbAICommand(node, 'attack', plNode);
// set cd
this.curCd = this.cd;
}
} else {
// decrease cd in case it's not 0 now
this.curCd -= dt;
}


I'm not sure if this exactly code will work, but I believe, you got the idea.

In case you're trying to make something more complex I recommend you to use state machine pattern. You can find a great js tutorial on AI state machine here: https://youtu.be/EfYs3I2yfts?list=PLNwtXgWIx3rg3J4XyuDVhjU81dZbJtVAU. It's for phaser, not ccb, but it's good to get the idea.

Unfortunately, there is other part of this related to path finding - I can't recommend anything here. You can, probably, check https://github.com/Mugen87/yuka for inspiration.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Intern?tional" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact