Hello guys, i am trying to add a follow path function for NPC’s but i’m doing something wrong. I explain first the whole situation. In some quest you will have a npc ally that will follow you, so when the player go more far than a given length the ally will follow him, if it’s more far than length*1.5 the npc will run after the player to catch up. But when in dungeon there will be walls, so if the npc is struck against a wall he have to follow a path to reach the player. So i have this code for the path find
pathFind: function () {
if (this.test===1) {
this.Root.script.gui.testpaper();
}
var x = this.Player.script.player.x;
var y = this.Player.script.player.y;
//Create a path
if (this.path.length===0 || (this.path.length>0 && this.x===this.path[0][0] && this.y===this.path[0][1])) {
var prevX = this.x;
var prevY = this.y;
this.path = this.game.pathToAFromB(x+","+y, this.x+","+this.y);
}
//Move towards the player
if(this.path.length >1){
this.dx = this.path[0][0];
this.dy = this.path[0][1];
//Don't move through closed doors
if(!this.game.doorLocations[this.Player.script.player.depth-1][x+','+y] || !this.game.doorLocations[this.player.depth-1][x+','+y].closed){
if (this.x!==this.dx || this.y!==this.dy) {
this.dest=new pc.Vec3(this.dx,0,this.dy);
this.pathTo(this.dest);
this.startx=this.x;
}
}
this.state="path";
}
}
then i have this line to trigger the pathfind
if ((this.state === "walking" || this.state==='run' ) && speed<0.06) {
if (this.dx!==0 && this.dy!==0) {
if (this.x!==this.dx || this.y!==this.dy) {
this.pathFind();
}
}
}
but something doesn’t work as it should, any idea?