I am making a space shooter game and I would like my ship to move and rotate like a normal ship/plane. Pitch up and down already done, but I can not seem to figure out how to roll right and left, only regular turning. Basically I want to be able to do a barrel roll as the ship but the only way I know how is to use the rotateLocal(x, y, z) and this is both not smooth and does not work while pressing other input keys. I am using applyTorque(force_right) for example for my movement and it works very well, but I can only use this for up, down, left, and right, so no rotation. I am a beginner coder and I have only been doing this for about a week now so if anyone has a solution to my problem I would be ever so thankful.
Game: https://launch.playcanvas.com/1142404?debug=true
TLDR; How do I add a movement script to my ship to make it barrel roll?
Base code:
var Fly = pc.createScript('fly');
Fly.attributes.add('speed', { type: 'number', default:500 });
Fly.attributes.add('camera', {type: 'entity'});
//
// initialize code called once per entity
Fly.prototype.initialize = function() {
this.roll = 0;
};
Fly.prototype.localCameraUpdate = function(){
// Change camera FOV based on speed of ship
var cameraEntity = this.camera;
cameraEntity.camera.fov = 55 + (this.entity.rigidbody.linearVelocity.length() * 0.1);
// Make camera rotate when moving left/right
var rollSpeed = 0.75;
var rollMax = 30;
if(this.app.keyboard.isPressed(pc.KEY_RIGHT)){
this.roll += rollSpeed;
//if(this.roll > rollMax) this.roll = rollMax;
}
else if(this.app.keyboard.isPressed(pc.KEY_LEFT)){
this.roll -= rollSpeed;
//if(this.roll < -rollMax) this.roll = -rollMax;
}
this.roll *= 0.95;
cameraEntity.setRotation(this.entity.getRotation()); // reset rotation
cameraEntity.rotateLocal(-10, 0, this.roll);
var pos = new pc.Vec3(0,3,10);
var newPos = new pc.Vec3(0,4,12);
var speedFactor = this.entity.rigidbody.linearVelocity.length() / 170;
pos.lerp(pos, newPos, speedFactor);
cameraEntity.setLocalPosition(pos);
};
// update code called every frame
Fly.prototype.update = function(dt) {
// Press Z to thrust
if(this.app.keyboard.isPressed(pc.KEY_W)) {
//Move in the direction its facing
var force = this.entity.forward.clone().scale(this.speed);
this.entity.rigidbody.applyForce(force);
}
// Update the camera
this.localCameraUpdate();
// P to pause
if(this.app.keyboard.wasPressed(pc.KEY_P)){
if(this.app.timeScale == 1){
this.app.timeScale = 0;
} else {
this.app.timeScale = 1;
}
}
// Rotate up/down/left/right
if(this.app.keyboard.isPressed(pc.KEY_DOWN)){
var force_up = this.entity.right.clone().scale(30);
this.entity.rigidbody.applyTorque(force_up);
}
if(this.app.keyboard.isPressed(pc.KEY_UP)){
var force_down = this.entity.right.clone().scale(-30);
this.entity.rigidbody.applyTorque(force_down);
}
if(this.app.keyboard.isPressed(pc.KEY_RIGHT)){
// Rotate to the right
var force_right = this.entity.up.clone().scale(-50);
this.entity.rigidbody.applyTorque(force_right);
}
if(this.app.keyboard.isPressed(pc.KEY_LEFT)){
var force_left = this.entity.up.clone().scale(50);
this.entity.rigidbody.applyTorque(force_left);
}
};