If you look closely at the player character in my project, you’ll see he’s moving at a slight angle, and I’m not sure why. I need him to move exactly on the x-axis, but I’m not sure what to change in the script to fix it.
Here’s the portion of the script that controls actual movement:
Movementone.prototype.update = function(dt) {
var forceX = 0;
var forceZ = 0;
// calculate force based on pressed keys
if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
forceX = -this.speed;
}
if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
forceX += this.speed;
}
this.force.x = forceX;
this.force.z = forceZ;
// if we have some non-zero force
if (this.force.length()) {
// calculate force vector
var rX = Math.cos(-Math.PI * 0.25);
var rY = Math.sin(-Math.PI * 0.25);
this.force.set(this.force.x * rX - this.force.z * rY, 0, this.force.z * rX + this.force.x * rY);
// clamp force to the speed
if (this.force.length() > this.speed) {
this.force.normalize().scale(this.speed);
}
}
// apply impulse to move the entity
this.entity.rigidbody.applyImpulse(this.force);
Here’s the project:
https://playcanvas.com/project/1041189/overview/expungeds-unfair-platformer-wip