Simple script not working

I am starting a new project and am simply trying to add keyboard arrow key movement to an entity (box)
I have attached the script to the box object and gotten no error messages. I’ve checked the code in the Code editor and it has no error messages for me. When I Launch the project, it doesn’t give me an error.
But when I push the arrow keys nothing moves. I’m stuck.

Here is the script:

var Movement = pc.createScript(‘movement’);

Movement.attributes.add(‘speed’, {
type: ‘number’,
default: 0.1,
min: 0.05,
max: 0.5,
precision: 2,
description: ‘Controls the movement speed’
});

// initialize code called once per entity
Movement.prototype.initialize = function() {
this.force = new pc.Vec3();
};

// update code called every frame
Movement.prototype.update = function(dt) {
var forceX = 0;
var forceZ = 0;

// calculate force based on pressed keys
if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
    forceX = -this.speed;
} 

if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
    forceX += this.speed;
}

if (this.app.keyboard.isPressed(pc.KEY_UP)) {
    forceZ = -this.speed;
} 

if (this.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);

};

Can you provide a link to the project please?

https://playcanvas.com/project/529844/overview/whatever

In order for forces to work on your object, it needs to have a collision component. You can go to add component -> collision to add one. This is so that physics engine knows how large it is and how to handle interactions with other objects.

Ok, so I added the collision component and I still can’t control the box. However now it falls through the plane and disappears. If I switch it to kinematic or static it won’t fall, but I still can’t get the script to work. I still can’t control the box.

So if the box falls through the plane you can do one of two things:

  1. Add a collision component and rigidbody component to the plane, and set the rigidbody type of the plane to static. This way it won’t move and the box won’t fall through it.

  2. Go to the project settings, and under the physics tab set gravity to 0.

Hope this helps!

All of your input definitely helps. Its clear I have a lot to learn.
I followed your instructions but if I set the plane to static and make it a rigidbody with a collision component, the box still falls through it. However it falls in a new way. Instead of falling straight down, it appears to balance on something invisible and then falls (rotates) off of it like something falling off a table, and then it falls through the plane. I do have the ability to move it for a moment though before it falls through the plane and is gone.

Make sure that your collision component is the right size, and covers your entire model. The collision area is shown highlighted in blue.

Thank you. That’s what I was missing!