[SOLVED] Questions about FirstPersonMovement script from tutorials

FirstPersonMovement.prototype.update = function(dt) {
    // If a camera isn't assigned from the Editor, create one
    if (!this.camera) {
        this._createCamera();
    }
.........
}

Why are we checking if a camera exits in the update loop? Wouldn’t it be better (performance-wise) to only check for it once when the script initializes?

Also,

var x = 0;
var z = 0;

// Use W-A-S-D keys to move player
// Check for key presses
if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_Q)) {
   x -= right.x;
   z -= right.z;
}

Why are we doing

x -= right.x;

instead of

x = -right.x;

Good point, yes it can be done once as well, if you don’t plan on adding/removing the camera.

Because we won’t to subtract the value from the current one, not set it to be equal to -right.x. So we can use multiple if clauses to allow multiple keyboard buttons to pressed at once, so you can move forward and left at the same time for example.

1 Like