Action that makes one Node follow another (moving) Node

Hi, i am looking for a method to create a magnet effect. I want 1 node to move directly to the position of another node, even if that node moves, ie update the trajectory on the fly.

I want a coin to fly to the player even as the player moves on the screen like it is magnetized towards the player.

The player has a standard circle collider and the coin has a physics collider and ridged body. the coin falls from the top of the screen to the player who is at the bottom. As it enters the magnetic radius of the player i want the coin to get sucked into the player. I cant get it to work.

// magnetRadius script
    onCollisionEnter: function (other, self) {
    	
    	
    console.log('on collide ENTER magnetRadius with coin');

    // the magnet radius is a child of the player node
    console.log(this.node.getParent().getComponent('PlayerPrefab').node.x);
    	 
    console.log(this.node.getParent().getComponent('PlayerPrefab').node.y);

       other.node.getComponent('collectablesPrefab').followMe(this.node.x, this.node.y);

    },

// coin script
followMe: function(x,y){
	 	
	var actionBy = new cc.MoveTo(0.2, cc.p(x+30, y));	
	this.node.runAction(actionBy);
	 
	this.follwMe = true;
		
	},
	stopFollowMe: function(){
	//this.node.physicsBody.sensor = false;
	 		
	 this.follwMe = false;
		
	}, 

Using the above method makes the coins bounce off the player magnetic radius to near the middle of the screen and then shoot down.

I tried to turn off the physics body for that node using this code;
this.node.physicsBody.sensor = false;

however, it returned an error saying cannot set value sensor of undefined.

How can i achieve this in javascript. I’m using cocos creator so bare that in mind please.

I found this resource which does exactly as i want but it is not in javascript… Can any one assist with this please?

Thanks in advance :slight_smile:

@jare can you review this please and give advice?

Can anyone achieve this in JS?