Odd error with moveToObject

I have a script for moving my game units with a click on game tiles like so:

var selectedArray = [ // ARRAY OF MY UNITS AND THEIR DATA IN HERE ] 

this.input.setHitArea(tileGroup.getChildren()).on('gameobjectdown', function(pointer, gameObject, scene) {

		if (attackReady == true) {

			for (var i = 0; i < selectedArray.length; i++)
			{	
				const thisUnit = Units[selectedArray[i]];

                theScene.physics.moveToObject(thisUnit, pointer, 200);

            }

       }

});

but I am receiving this error in chrome:

“gameObject.body.velocity.setToPolar is not a function
at ArcadePhysics.moveTo (phaser.js:196815)”

which doesnt make sense as I am not calling velocity or setToPolar anywhere in here…

But moveToObject does :slight_smile:
And since ‘gameobjectdown’ will work on all game objects, if you click something that is not a physics object, you will get this error.

my tiles are matter objects, not physics, like so:

if (tileType==0)
{
thisTile = this.matter.add.sprite(centerX + tx, centerY + ty, 'flatsprite',0, { shape: { type: 'fromVerts', verts: iso, flagInternal: true } }).setStatic(true).setSensor(true); 
}

so should I be using a different type of movement for my gameobjects, ie: one that works with both matter and physics, or is there something else I should be doing to make this work given my scenario?

See a previous topic:

Create your own moveToObject function (by copying the Arcade code).
Use Vector2 setToPolar and Matter setVelocity.

If you want it to work for both physics types, you can check if moveToObject exists, else use your own function. I guess you also shouldn’t use ‘gameobjectdown’, since that can mean anything, like UI or whatever. I would attach ‘pointerdown’ to each Child (or at least check what I clicked on).