Code problem from a merge request on GitHub

So the default keys on my game are arrow keys.
A player in one of my discord servers made a merge request to change it from arrow to wasd, I did the code but it didn’t work for me but it worked for him what did I do wrong
current code

// Controls the movement speed
pc.script.attribute('speed', 'number', 0.1, {
    min: 0.05,
    max: 0.5,
    step: 0.05,
    decimalPrecision: 2
});

// Script Definition
pc.script.create('movement', function (app) {
    
    // Creates a new Movement instance
    var Movement = function (entity) {
        this.entity = entity;
        this.force = new pc.Vec3();
    };

    Movement.prototype = {        
        // Called every frame, dt is time in seconds since last update
        update: function (dt) {
            var forceX = 0;
            var forceZ = 0;
            
            // calculate force based on pressed keys
            if (app.keyboard.isPressed(pc.KEY_LEFT)) {
                forceX = -this.speed;
            } 
            
            if (app.keyboard.isPressed(pc.KEY_RIGHT)) {
                forceX += this.speed;
            }
            
            if (app.keyboard.isPressed(pc.KEY_UP)) {
                forceZ = -this.speed;
            } 
            
            if (app.keyboard.isPressed(pc.KEY_DOWN)) {
                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);
                }
            }
            
            // apply impulse to move the entity
            this.entity.rigidbody.applyImpulse(this.force);
        }
    };

    return Movement;
});

merge request link
https://github.com/SHADOWELITE7/Rolling-ball-game/pull/1

Looks like you are using the Legacy Scripting system (no longer being worked on) and your contributor is using the current Scripting system (known as Scripts 2.0).

I forgot to ask, how many versions of JavaScript are there because I am not sure what version of js sololearn uses

Just to make the distinction clear, when I talk about the scripting system, I’m referring to PlayCanva’s Scripting Component System and not Javascript.

PlayCanvas has two systems:

In terms of Javascript version, PlayCanvas is written ES5 IIRC in order to be as compatible as possible with browsers. It’s possible to use ES6 though as mentioned here: How to enable es6?

1 Like