[SOLVED] Entity function or key listeners not working

PROJECT LINK: https://playcanvas.com/editor/scene/1142476

SCRIPT LINK:
https://playcanvas.com/editor/code/790861?tabs=46937720

I have not debugged it myself.

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

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

// update code called every frame
Movement.prototype.update = function(dt) {
    
    var entity = new pc.Entity();
    
    var p = this.entity.getPosition();

    if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
    entity.setPosition(p.x + 1, p.y, p.z);
    }
};

// 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:
// https://developer.playcanvas.com/en/user-manual/scripting/

As you can see here, I am trying to create a movement script, in which i take the position of the entity, and on the vent of a key press, modify the positions of the entity, just like movement. It has not worked though. No error messages, just not working. I really need help.

Hey, you are updating the position of a new entity that is not even in the hierarchy. You should change the last line to this.entity.setPosition(p.x + 1, p.y, p.z); and it should work.

1 Like

This reply was really fast!
I’ll give it a try!

Thank you!

It works!