Create a path for NPCs

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) {
            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 =","+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.Player.script.player.depth-1][x+','+y] || ![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);

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) {

but something doesn’t work as it should, any idea?

If you are trying to implement proper path finding with obstacle avoidance, where your NPCs can navigate around your game static or dynamic objects that’s a complex subject with multiple solutions (grid based vs navmesh based).

Try searching for javascript pathfinding, usually you can get away with using a simple 2D grid even in 3D games.

Now if you are only trying to make the NPC move towards the Player in a straight line (without navigating around walls, doors etc) … you will need to calculate the path from A to B and move the NPC entity using some kind of animation. I see you have a pathToAFromB but I don’t see the implementation.

Hi, yes i forgot the routine AtoB since it works fine and the this.path array is properly filled, the problem is that the player don’t move alongside the path

pathToAFromB: function(a, b){
            var partsA = a.split(",");
            var xA = parseInt(partsA[0], 10);
            var yA = parseInt(partsA[1], 10);
            var partsB = b.split(",");
            var xB = parseInt(partsB[0], 10);
            var yB = parseInt(partsB[1], 10);
            var self=this;
            var passableCallback = function(x, y){
            var astar = new ROT.Path.AStar(xA, yA, passableCallback, {topology:4});
            var path = [];
            var pathCallback = function(x, y){
                path.push([x, y]);
            astar.compute(xB, yB, pathCallback);
            return path;

Your code isn’t compete, you are missing the actual animating method to move the entity from point to point.

Here is a good tutorial on how to do that:

You are right lol Forgot a part of code completely :stuck_out_tongue: Thanks