How Do I Move a Player And Camera

Hey Everyone. I was wondering what do i type in the script to move a player and the camera with the arrow keys. i am new at this and really want to make my first game. Thanks For the help!!

You can assign this script to any entity to move it around:

    var Movement = pc.createScript('movement');

    Movement.attributes.add('speed', { type: 'number', default: 1 });

    // initialize code called once per entity
    Movement.prototype.initialize = function() {
        
    };

    // update code called every frame
    Movement.prototype.update = function(dt) {
        var left = this.app.keyboard.isPressed(pc.KEY_LEFT);
        var right = this.app.keyboard.isPressed(pc.KEY_RIGHT);
        var up = this.app.keyboard.isPressed(pc.KEY_UP);
        var down = this.app.keyboard.isPressed(pc.KEY_DOWN);

        if (left) {
            this.entity.translate(-this.speed * dt, 0, 0);
        }
        if (right) {
            this.entity.translate(this.speed * dt, 0, 0);
        }
        if (up) {
            this.entity.translate(0, 0, -this.speed * dt);
        }
        if (down) {
            this.entity.translate(0, 0, this.speed * dt);
        }
    };

    // swap method called for script hot-reloading
    // inherit your script state here
    Movement.prototype.swap = function(old) {

    };

    // to learn more about script anatomy, please read:
    // http://developer.playcanvas.com/en/

Don’t forget to parse the script to show the ‘speed’ script attribute in the Editor UI.

Do you also have a gravity script???

If you want gravity applied to objects in your scene, you might want to take a look at the PlayCanvas physics engine.

Here is some introductory information:

http://developer.playcanvas.com/en/user-manual/physics/

And a simple tutorial:

http://developer.playcanvas.com/en/tutorials/beginner/Using-forces-on-rigid-bodies/

Instead of moving a physics object around with the translate function, you’d use forces/impulses instead.

If you use the script that @will gave you, then you can put it on the player and make the camera a child. That’s what I did here.

1 Like

is thia js cuz thats what it works in making a game