How could we hold and move an entity from mobile touch inputs?

How could we hold and move an entity from mobile touch inputs?

I have written following script:

MoveForward.prototype.onTouchMove = function (event) {

    //if(MoveForward.canPlay == false) return;

    for (var i = 0; i < event.changedTouches.length; i++) {

        var touch = event.changedTouches[i];

        if (this.touchId == touch.id) {

           

            var pos;

            var speed = 0.01;

            pos = this.entity.getPosition();

            if (touch.x < this.lastTouchPoint.x) {

                console.log('Movement left');

                pos.x = 5;

            }

           

            if (touch.x > this.lastTouchPoint.x) {

                console.log('Movement right');

                pos.x = -5;

            }

            this.vec.lerp(this.vec, pos, speed);

            this.entity.setPosition(this.vec);

            this.lastTouchPoint.set(touch.x, touch.y);

            this.timer = 0;

       

        }

    }

};

Hi @yash_mehrotra and welcome! Is your current script not working? For forward and backward I expect you could do the same as you do for left and right.

@Albertos In current script the movement is not smooth. Entity starts jittering as I hold my touch while moving left and right on mobile device.

Can you share a link of your project so someone can take a look?

It looks like left and right are in conflict with each other. I suggest building in some margin.

@Albertos What do you mean by margin? Could you give an example?

The difference between left and right is nil, so you have to build a kind of space in between where nothing happens.