Problems with collision detection

Hi, I need to detect collision between two game objects but event when they ‘touch’ each other on screen, the collider callback doesn’t seem to work.
This is my code:

const move1 = setInterval(() => {
      container1.x += 1;
    }, 20);

    const move2 = setInterval(() => {
      container2.x -= 1;
    }, 20);

    this.gameInstance.physics.add.overlap(container1, container2,() => {
      console.log('overlap detected..')
    this.gameInstance.physics.add.collider(container1, container2, () => {
      console.log('COLLISION DETECTED')


Don’t use setInterval(). Use the scene update() function instead.

Collisions needs velocity to work properly. Don’t move the x and y of physics game objects. Give them velocity instead.


Thank you.
I used the setVelocity, but i need the 2 objects to move towards each other on the X axis, so I have to move their x. With only setVelocityX nothing happens.

Show your code with velocity