Ambiera ForumDiscussions, Help and Support. |
|
|
|||||
|
How do I make melee enemy that follows you using javascript |
||||
|
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:
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. |
|