# Problem with physics.moveToObject, object go out

**URL:** <https://phaser.discourse.group/t/problem-with-physics-movetoobject-object-go-out/8239>\
**Category:** Phaser 3\
**Created:** [November 29, 2020, 12:54pm UTC](https://phaser.discourse.group/t/problem-with-physics-movetoobject-object-go-out/8239 "2020-11-29T12:54:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [November 29, 2020, 12:54pm UTC](https://phaser.discourse.group/t/problem-with-physics-movetoobject-object-go-out/8239/1 "2020-11-29T12:54:03Z")

</div>

hi,

When i use this example :

> **[Phaser 3](https://labs.phaser.io/view.html?src=src%5Cphysics%5Carcade%5Cmove%20and%20stop%20at%20position.js)**

I click and the sprite go the position.

But when i do the same with a window.setTimeOut the sprite go out my target…why ?  
Thanks for your help.

Below the snippet.

```
var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    parent: 'phaser-example',
    physics: {
        default: 'arcade'
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};  

var debug;
var source;
var target={}
var distanceText;

new Phaser.Game(config);

function preload ()
{
    this.load.image('flower', 'assets/sprites/flower-exo.png');
}

function create ()
{
    source = this.physics.add.sprite(100, 300, 'flower');

    debug = this.add.graphics();

    this.input.on('pointerdown', function (pointer) {
        console.log("on")

        target.x = pointer.x;
        target.y = pointer.y;
        
        // Move at 200 px/s:
        this.physics.moveToObject(source, target, 200);

        debug.clear().lineStyle(1, 0x00ff00);
        debug.lineBetween(0, target.y, 800, target.y);
        debug.lineBetween(target.x, 0, target.x, 600);

    }, this);

    distanceText = this.add.text(10, 10, 'Click to set target', { fill: '#00ff00' });
    window.setTimeout(()=>{
        let target={}
        target.x = 100
        target.y = 100
                this.physics.moveToObject(source, target, 200);
                console.log(target.y)

    },5000)
}

function update ()
{
    var distance = Phaser.Math.Distance.Between(source.x, source.y, target.x, target.y);

    if (source.body.speed > 0)
    {
        distanceText.setText('Distance: ' + distance);

        // 4 is our distance tolerance, i.e. how close the source can get to the target
        // before it is considered as being there. The faster it moves, the more tolerance is required.
        if (distance < 4)
        {
            source.body.reset(target.x, target.y);
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [November 30, 2020, 4:04pm UTC](https://phaser.discourse.group/t/problem-with-physics-movetoobject-object-go-out/8239/2 "2020-11-30T16:04:05Z")

</div>

hi,

Nobody ?

I made a jsfiddle to prove my bug:

> **[Edit fiddle - JSFiddle - Code Playground](https://jsfiddle.net/espace3d/b9085mjp/)**

the first window.setTimeout don’t works and the second with 5000ms works perfectly and however the code is the same…could you tell me where is the mistake ?

```
var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    parent: 'phaser-example',
    physics: {
        default: 'arcade'
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};  

var debug;
var source;
var target={}
var distanceText;
    var to={}

new Phaser.Game(config);

function preload ()
{
    this.load.image('flower', 'assets/sprites/flower-exo.png');
}

function create ()
{
    source = this.physics.add.sprite(100, 300, 'flower');

    debug = this.add.graphics();

    this.input.on('pointerdown', function (pointer) {
        console.log("on")

        target.x = pointer.x;
        target.y = pointer.y;
        
        // Move at 200 px/s:
        this.physics.moveToObject(source, target, 200);

        debug.clear().lineStyle(1, 0x00ff00);
        debug.lineBetween(0, target.y, 800, target.y);
        debug.lineBetween(target.x, 0, target.x, 600);

    }, this);

    distanceText = this.add.text(10, 10, 'Click to set target', { fill: '#00ff00' });
    window.setTimeout(()=>{
            // let target={}
            target.x = 100
            target.y = 100
            this.physics.moveToObject(source, target, 200);
    },5000)

    window.setTimeout(()=>{
            to.x =200
            to.y =200
            this.physics.moveToObject(source, to, 200);
    },1500)
}

function update ()
{
    var distance = Phaser.Math.Distance.Between(source.x, source.y, target.x, target.y);

    if (source.body.speed > 0)
    {
        distanceText.setText('Distance: ' + distance);

        // 4 is our distance tolerance, i.e. how close the source can get to the target
        // before it is considered as being there. The faster it moves, the more tolerance is required.
        if (distance < 4)
        {
            source.body.reset(target.x, target.y);
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [December 1, 2020, 4:45pm UTC](https://phaser.discourse.group/t/problem-with-physics-movetoobject-object-go-out/8239/3 "2020-12-01T16:45:51Z")

</div>

really nobody about that ?

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [December 2, 2020, 6:38pm UTC](https://phaser.discourse.group/t/problem-with-physics-movetoobject-object-go-out/8239/4 "2020-12-02T18:38:58Z")

</div>

always nobody please ?

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [December 2, 2020, 8:37pm UTC](https://phaser.discourse.group/t/problem-with-physics-movetoobject-object-go-out/8239/5 "2020-12-02T20:37:43Z")

</div>

Hi @espace,  
To stop the object you have to calc the distance between the object and the target:

```javascript
var distance = Phaser.Math.Distance.Between(source.x, source.y, target.x, target.y);

```

And then reset the object position when the distance is almost 0:

```javascript
if (distance < 4)
        {
            source.body.reset(target.x, target.y);
        }

```

In your first post, the **target** variable declared in setTimeout function is not the same than in the update function, and in your second post, the distance between **source** and **to** is never calculated.

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [December 3, 2020, 2:59am UTC](https://phaser.discourse.group/t/problem-with-physics-movetoobject-object-go-out/8239/6 "2020-12-03T02:59:44Z")

</div>

Hi @jjcapellan,

Sometimes we don’t see the stupid thing that keeps you from moving forward and I do.

Thanks for your light.
