Control the "Player" movement (Translate method)

https://playcanvas.com/editor/scene/1407296

I can’t control the Player movement. In the code below, you can see the method of movement. The problem is, even when the mouse position is still fixed, “this.isright” continues to move because it remains false or true. How do I solve this?

var Movement = pc.createScript('movement');

Movement.attributes.add('camera', {type : 'entity'});

Movement.prototype.initialize = function() {

    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    var isMouseEnter = false;
    var lastX;
    var isRight;
    var hitBlock;

    hitBlock = false;

    if (this.app.mouse){
        this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
        this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
        this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
    }
};

Movement.prototype.onMouseDown = function(event) {
    this.isMouseEnter = true;
    this.lastX = event.x;
};

Movement.prototype.onMouseMove = function(event, dt) {
    if (this.isMouseEnter){
        if (event.x > this.lastX){
            this.isRight = true;
            this.lastX = event.x;
        }
        if (event.x < this.lastX){
            this.isRight = false;
            this.lastX = event.x;
        }
    }
};

Movement.prototype.onMouseUp = function(event) {
    this.isMouseEnter = false;
};

Movement.prototype.update = function(dt) {
    if (!this.hitBlock){
        this.entity.translate(0, 0, dt * -5);
        this.camera.translate(0, 0, dt * -5);
        if (this.isMouseEnter){
            if (this.isRight === true && this.entity.getPosition().x < 3.5) {
                this.entity.translate(8*dt, 0, 0);
                this.camera.translate(8*dt, 0, 0);
            }
            if (this.isRight === false && this.entity.getPosition().x > -3.5){
                this.entity.translate(-8*dt, 0, 0);
                this.camera.translate(-8*dt, 0, 0);
            }
        }
    }
};

Movement.prototype.onCollisionStart = function (result) {
    if (result.other.name === "Block1"){
        this.hitBlock = true;
    }
};

Right now you don’t have a ‘in between’ option, so you’ll need to build in some margin between to the right and not to the right. Something like I did below, where the margin is 0.5.

image