Why does it works when I check using pc.KEY_W but doesn’t work with key, even if key and pc.KEY_W have same value?
var Ball = pc.createScript('ball');
Ball.attributes.add('speed', {
type: 'number',
default: 15,
min: 0.05,
max: 30,
precision: 2,
description: 'Controls the movement speed'
});
// initialize code called once per entity
Ball.prototype.initialize = function () {
// this.app.keyboard.on(pc.EVENT_KEYDOWN, console.log, this)
};
// update code called every frame
Ball.prototype.update = function (dt) {
const forces = {
[pc.KEY_W]: new pc.Vec3(1, 0, 0),
[pc.KEY_A]: new pc.Vec3(0, 0, 1),
[pc.KEY_S]: new pc.Vec3(1, 0, 0),
[pc.KEY_D]: new pc.Vec3(0, 0, 1),
[pc.KEY_UP]: new pc.Vec3(1, 0, 0),
[pc.KEY_LEFT]: new pc.Vec3(0, 0, 1),
[pc.KEY_DOWN]: new pc.Vec3(1, 0, 0),
[pc.KEY_RIGHT]: new pc.Vec3(0, 0, 1),
}
const force = new pc.Vec3(0, 0, 0);
for (const key in forces) {
if (this.app.keyboard.isPressed(key)) {
// doesn't go here
force.add(forces[key]);
} else {
// console.log(key);
}
if (this.app.keyboard.isPressed(pc.KEY_W)) {
// but goes here
console.log("W pressed");
if (pc.KEY_W == key) {
console.log("Why it's not working at line 37")
}
}
}
if (this.app.keyboard.isPressed(pc.KEY_W)) {
// force.add(new pc.Vec3(1, 0, 0));
}
force.mulScalar(this.speed)
this.entity.rigidbody.applyForce(force);
};
// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// Ball.prototype.swap = function(old) { };
// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/