I have a script problem - Cannot read property keyboard

i am switching some scripts and taking some out, and it broke? ive been trying to fix it
it has a probem with the Keyinding

   // Use W-A-S-D keys to move players
    // Check for key presses
    if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_LEFT)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D) || app.keyboard.isPressed(pc.KEY_RIGHT)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W) || app.keyboard.isPressed(pc.KEY_UP)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S) || app.keyboard.isPressed(pc.KEY_DOWN)) {
        x -= forward.x;
        z -= forward.z;
    }

here is link to the scene
https://playcanvas.com/editor/scene/1215638

Hi @The_real_bosser24! Normally you should use this.app but the original script added the line below which you are missing at the moment.

var app = this.app;
2 Likes

is this what you wanted,@Albertos ?

 // Use W-A-S-D keys to move players
    // Check for key presses

var app = this.app;


    if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_LEFT)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D) || app.keyboard.isPressed(pc.KEY_RIGHT)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W) || app.keyboard.isPressed(pc.KEY_UP)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S) || app.keyboard.isPressed(pc.KEY_DOWN)) {
        x -= forward.x;
        z -= forward.z;
    }

Exactly.

1 Like

So, I pressed Play, And it pulls p the same thing as the begining, so i press orange link and this is what happened

and this is the screen

Did you save your changes?

1 Like

yes, I have.
Do you want the whole script?
Do i need to do something before the Keyboard Function

I see you placed the code outside the function. The code from your first post of this topic should be between line 52 and 53 of the script.

2 Likes


another problem…
it says something about a camera
the keyboard problem is fixed
do you think its just placement?

The code from line 82 to line 99 should be before line 60 (and you can remove line 57 because this will be a duplicate).

Apart from that, you can’t take scripts apart without the necessary knowledge, because things are connected. So you’ll get a lot more erros if you don’t know what you’re doing.

i fixed all that, shows no more errors, but now the camera dont move

To be honest, This is Confusing

I see your script attributes are not visible on the script component. Select your Capsule entity and use the parse button of the script component. Make sure you add your camera entity to the attribute for the camera.

2 Likes

how do i do the camera thing

Which camera thing?

1 Like

this

You have to fix the script problem first. Remove the movement.js script that not exist anymore from your Cylinder entity and add the Movement.js script. See if your attributes of the script are available and drag your camera entity into the correct attribute. Otherwise you need to parse the script first.

2 Likes


it works, but this happened

i think this is the problem

You forget to do this.

what is an attribute?
Can u please dumb it down