Swerve movement is a little glitchy

I have a following swerve code to rotate and translate an entity, but it seems a little glitchy.
What am I missing here?

        let swipeDelta = 0;
        if (this.touching && this.isGrounded) 
        {
            swipeDelta = this.currentPosition.x - this.startPosition.x;
            this.startPosition.copy(this.currentPosition);
            this.currentRotation.copy(this.entity.getLocalEulerAngles());

            // clamp y rotation
            let angle = this.currentRotation.y - swipeDelta * this.rotationY * dt;
            angle = pc.math.clamp(angle, -30, 30);
            this.entity.setLocalEulerAngles(0, angle, 0);

            // Move the entity forward in its local space
            var forward = this.entity.right;

            // Gradually increase the speed
            this.moveSpeed = Math.max(this.moveSpeed + this.acceleration, this.maxSpeed); 
            var moveAmount = forward.scale(this.moveSpeed * dt);
            var currPos = this.entity.getLocalPosition();
            var newPos = currPos.clone().add(moveAmount);

            // Clamp the Z position within the specified range
            newPos.z = pc.math.clamp(newPos.z, this.minZ, this.maxZ);
            this.entity.setLocalPosition(newPos);
        }
Swerve.prototype.onTouchStart = function(event) {

    if(!this.canPlay || this.gameOver) return;

    const touches = event.touches;
    this.touching = touches.length === 1;
    if (this.touching) {
        this.startPosition.set(touches[0].x, touches[0].y);
        this.currentPosition.set(touches[0].x, touches[0].y);
    }

    event.event.preventDefault();
};
Swerve.prototype.onTouchMove = function(event) {

    if(!this.canPlay || this.gameOver) return;

    const touches = event.touches;
    if (this.touching) {
        this.currentPosition.set(touches[0].x, touches[0].y);
    }

    event.event.preventDefault();
};

Changed and moved the logic for rotation in touch move event. Works well now.

Swerve.prototype.onTouchMove = function(event) {

    if(!this.canPlay || this.gameOver) return;

    var touchX = event.touches[0].x;
    // var screenWidth = this.app.graphicsDevice.width;
    var screenWidth = window.innerWidth;

    var normalizedX = (touchX / screenWidth) * 2 - 1; // Normalize touch position to range [-1, 1]
    var targetRotation = normalizedX * -30; // Rotate up to 30 degrees based on touch position

    targetRotation = pc.math.clamp(targetRotation, -30, 30); // Clamp rotation between -30 and 30 degrees

    this.entity.setLocalEulerAngles(0, targetRotation, 0);

    event.event.preventDefault();
};
1 Like