Code error help

Hello all people! I created this thread so the people can comment on here to ask the same type of question I have, why does my script give this error, and can someone help me fix it? I will go ahead with my error and script.

This is my Run script

var Run = pc.createScript('run');

// initialize code called once per entity
Run.prototype.initialize = function() {
    // get the current position of the player
    this.initialPosition = this.entity.getPosition();
};

// update code called every frame
Run.prototype.update = function(dt) {
    // check if the left shift key is pressed
    if (pc.input.isKeyPressed(pc.KEY_LSHIFT)) {
        // move the player forward by 1 unit per frame
        this.entity.setPosition(this.initialPosition.x + 1, this.initialPosition.y, this.initialPosition.z);
    }
};

It gives me this error: [Run.js?id=115194781&branchId=3dc2247a-c4ce-4dac-8229-7ec0d0262a7c:12]: pc.input.isKeyPressed is not a function

I’m pretty sure I did everything right but I don’t know why the script won’t work and keeps giving the error.

@Jacob_McBride1 I think the keyboard script needs to be something like the following.

this.app.keyboard.isPressed(pc.KEY_D))<<<<<<<<<<<<<<<< Your key here

1 Like
var Run = pc.createScript('run');

// initialize code called once per entity
Run.prototype.initialize = function() {
    // get the current position of the player
    this.initialPosition = this.entity.getPosition();
};

// update code called every frame
Run.prototype.update = function(dt) {
    // check if the left shift key is pressed
    if (pc.input.this.app.keyboard.isPressed(pc.KEY_LSHIFT)) {
        // move the player forward by 1 unit per frame
        this.entity.setPosition(this.initialPosition.x + 1, this.initialPosition.y, this.initialPosition.z);
    }
};

Hi @Jacob_McBride1,

Maybe you could shed some light on where you’re getting this code example/pseudo-code? It seems like you might be getting confused because you are using old tutorials, or tutorials from other engines. The for checking key presses in the update function looks like this:

if(this.app.keyboard.isPressed(pc.KEY_SHIFT)) {
...
}

What you are using doesn’t exist in the current build of the engine. If you are having trouble with keyboard input, feel free to check out this section of the User Manual:

https://developer.playcanvas.com/en/tutorials/keyboard-input/

The Editor’s code editor will also provide you with suggestions for key codes, and you can see a list of all of the key codes here in the API reference:

https://developer.playcanvas.com/api/pc.html#KEY_0

I hope this is helpful

Like that?

I am not familiar with the new functions and scripting, as I learned how to code JavaScript about 3 years ago using old tutorial videos so it is on me.

Right. My understanding is that there are a lot of old tutorial videos out there for the old scripting system that no longer works with current projects. I would suggest reviewing the Tutorials section and the User Manual to familiarize yourself with the structure of scripts and the tools that are currently available to you from the engine.

2 Likes

Right, I do need to review everything because this game engine has changed so much from the time I joined.