[SOLVED] Noob Q - How to apply gravity after initial load

Hey guys, I’ve been watching tutorials and looking at sample code and am attempting to create my first project with just a block that has gravity and can move with the arrow keys.

When it initially loads, the block falls down and hits the ground and bounces a little as expected.

Then I have the following script in the block object:

var Movetest = pc.createScript('movetest');
var keyboard = new pc.Keyboard(window);


// initialize code called once per entity
Movetest.prototype.initialize = function() {

// update code called every frame
Movetest.prototype.update = function(dt) {
    if (keyboard.wasPressed(pc.KEY_RIGHT)){
        this.entity.translateLocal(0.01,0,0);
    }
    if (keyboard.wasPressed(pc.KEY_LEFT)){
        this.entity.translateLocal(-0.01,0,0);
    }
    if (keyboard.wasPressed(pc.KEY_UP)){
        this.entity.translateLocal(0,-0.01,0);
    }
    if (keyboard.wasPressed(pc.KEY_DOWN)){
        this.entity.translateLocal(0,0.01,0);
    }
};

Now that works to move the block around the screen but if I walk off the ground, it doesn’t fall. Why doesn’t the gravity continue to effect the block after the initial fall when it’s loaded?

You shouldn’t use the transformation functions on the entity to move around an entity with a dynamic type rigid body component. Instead, you should use forces/impulses (API).

See these tutorials for more info. In particular, this one.

Ah okay thanks you so much for the help!

1 Like

Hey Will,

Finally got around to playing with that. So that works to keep the gravity working but it feels unnatural for the object to continue moving from the impulse after you stop pressing the keyboard. An example is i’m trying to make a character seem to walk around. When you stop pressing the arrow key, the character stops moving. How do I halt the block from moving after applying an impulse?

Nevermind, I figured it out with linear damping on the rigid body

1 Like