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();
};