Pathfinding (I need help!)

I cannot find anything regrading a pathfinder in playcanvas… So i came to the forums instead :slight_smile:

So, I’m making a tower defense game (as a project for school) and I decided to use PC for it, as it seemed suitable for what I’m doing, and I am mildly familar with JS, and can learn it rather quickly and complete the game before early 2019.

I have an example just to find out code for how I will spawn enemies, and I need pathfinding help.


enemy spawns at entity A
enemy needs to pathfind to entity B
then to C
D, E, F…
hit a teleporter, to another teleporter

pathfind to G, H, I etc…
teleporter again, then exit the screen as a “life lost”

I know, may sound confusing just reading it, but theres no better way to explain it really. So I’ll just ask for if I spawn a “box entity” on top of entity “A”, then I need this to pathfind itself to entity “B”. The rest I can figure out I would assume.

However, if you understood what I said above about all pathfinding, teleporters and any sort of code for spawning enemies and having them walk around the level, feel free to explain it to me, so I can take it with my own understanding :smiley:

I also need ideas for towers, however this will come much later, as I want a basic version first (1 tower, 1 wave, 5 enemies, bang). Then build off of that… (I’ll come back to this eventually)

Thanks for any replies in advance!

There isn’t a pathfinding library/feature that is native to PlayCanvas yet so you have to roll your own.

Typically, tower defence games are based on a grid like world which will you to apply most pathfinding algorithms like A Star (

Pathfinding is not a trivial area and will take a while to understand and implement.

I’ve figured something out now to do with


Now all I need is to see how to get the object to stop at a point, rotate, and continue to another point…

Would this work via entity.getPosition, then saying if Test.getPosition = entity.getPosition { this.entity.rotate(x); }


That will work for straight paths where there are no obstacles between the entity and the target position.

If you are going that route, then you can look at this example which does something similar:

The highlighted bit is what I’m questioning. Is there a way I can grab the position of another entity, call this A, and make it so that when these objects touch something occurs…

The way I thought of it was an if statement, but any form of entity.getPosition doesn’t want to work in order for me to use this.

The whole “Point and Click Movement” project isn’t exactly what I’m looking for I think, as the model I’m using is straight paths and 90’ rotations only.

Granted, but you are still moving to a target point at a constant speed and rotating the entity to face hat direction so the core logic is the same. The only difference is that you are using known target points vs a user defined one.

There’s also another sample where a camera follows a series of waypoints:

Yes there is.

var Foobar = pc.createScript('foobar');

Foobar.attributes.add('anotherObject', {type: 'entity'});

Foobar.prototype.someFunction = function () {
   var anotherObjectPosition = this.anotherObject.getPosition(); 

I’ll look at that one in a bit.

Before I do however, would saying

"if(this.entity.x === -1.5) { this.entity.rotate(90) }:wink:


Because in this case, I can make it with this concept instead of anything else.

Bare in mind that object is moving with constant velocity in horizontal&vertical directions

It will only work if this.entity.x is EXACTLY -1.5. Right down to the last decimal point.

You have to bear in mind of how floats are calculated and represented in memory as well. Doing the following:

var aNumber = 2 * -0.75;
if (aNumber === -1.5) {
    // This will never be true!

This will explain more:

How do you do a if (something is not equal to something else) ?

is it not =/ or something similar?

and how to while loops work?

KhanAcademy is a good place to learn the core basics:

That should cover comparisons and loops a lot more thoroughly than I could in a single forum post.

There should be also a tower defense game somewhere in playcanvas games, or you can check the roguelike projects

I couldn’t find anything. I’m trying to figure it now, and I’m at a point where I can get the object to move (easy enough)… but need to know if how to make it so the object stops at a certain x value…

I’m using this.entity.x < -1.532 (which it is up to -1.531) in a while loop, yet nothing happens.

While, if i put the value that “this.entity.x” should be (-3) in there, it moves the object, as -3 is always < -1.532 …

is this.entity.x doing nothing?

Just in case, I also said var y = this.entity.getPosition; //gathers the world position of the entity.

If this.entity.x is not the correct thing to use for this, what would be?

Edit: here if my while loop:

while (this.entity.x < -1.532) {

That property doesn’t exist as you can see from the entity API

If you want to get the world x position of the entity, then it would be this.entity.getPosition().x