Link to the Project:
https://launch.playcanvas.com/538672?debug=true
I want to move the Spaceship with WASD.
This works fine.
But I want also that the Spaceship looks in the direction it moves.
i dont know how to do this with rigibodies.
Thats my movement script atm:
var Movement = pc.createScript('movement');
Movement.attributes.add('speed', {
type: 'number',
default: 0.1,
min: 0.05,
max: 0.5,
precision: 2,
description: 'Controls the movement speed'
});
// initialize code called once per entity
Movement.prototype.initialize = function() {
this.force = new pc.Vec3();
};
// update code called every frame
Movement.prototype.update = function(dt) {
var forceX = 0;
var forceZ = 0;
var multiplikator = 1;
if (this.app.keyboard.isPressed(pc.KEY_SPACE)){
multiplikator = 2;
}
// calculate force based on pressed keys
if (this.app.keyboard.isPressed(pc.KEY_A)) {
forceX = -this.speed;
}
if (this.app.keyboard.isPressed(pc.KEY_D)) {
forceX += this.speed;
}
if (this.app.keyboard.isPressed(pc.KEY_W)) {
forceZ = -this.speed;
}
if (this.app.keyboard.isPressed(pc.KEY_S)) {
forceZ += 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 * multiplikator);
}
}
// apply impulse to move the entity
this.entity.rigidbody.applyImpulse(this.force);
this.entity.rigidbody.applyTorqueImpulse(this.force);
};