Gravity not applied while teleporting rigidbody

Hi, I’m creating a TPP multiplayer project. Everything works fine except gravity. Gravity is not applied to the rigid body when it is teleporting.

Screenshot 2022-05-02 at 3.29.05 PM

https://playcanvas.com/editor/scene/1405647

When I try to go down the stairs. Rigidbody stays in the air after I release the button it will automatically fall down to the ground

Here is the playermovement script -

var PlayerMovement = pc.createScript('playerMovement');

PlayerMovement.attributes.add('speed', { type: 'number', default: 0.09 });

PlayerMovement.prototype.initialize = function () {
    var app = this.app;
    var camera = app.root.findByName('Camera');
    this.cameraScript = camera.script.cameraMovement;    
    this.touchInput = new pc.Vec3();
    
};


// Temp variable to avoid garbarge colleciton
PlayerMovement.worldDirection = new pc.Vec3();
PlayerMovement.tempDirection = new pc.Vec3();

PlayerMovement.prototype.update = function (dt) {
    var app = this.app;
    var worldDirection = PlayerMovement.worldDirection;
    worldDirection.set(0, 0, 0);
    
    var tempDirection = PlayerMovement.tempDirection;
    
    var forward = this.entity.forward;
    var right = this.entity.right;

    var x = 0;
    var z = 0; 
    
    // console.log(pc);

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

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

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

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

    if (x !== 0 || z !== 0) {
        worldDirection.add(tempDirection.copy(forward).mulScalar(z));
        worldDirection.add(tempDirection.copy(right).mulScalar(x));        
        worldDirection.normalize();
        
        var pos = new pc.Vec3(worldDirection.x * dt, 0, worldDirection.z * dt);
        pos.normalize().scale(this.speed);
        pos.add(this.entity.getPosition());

        var targetY = this.cameraScript.eulers.x + 180;
        var rot = new pc.Vec3(0, targetY, 0);

        this.entity.rigidbody.teleport(pos, rot);
    }
    // console.log("dsad");
    // console.log(this.entity);
    
    this.entity.anim.setFloat('xDirection', x);
    this.entity.anim.setFloat('zDirection', z);

    // console.log(this.entity.anim.getFloat('xDirection'), this.entity.anim.getFloat('zDirection'));
    // console.log(this.entity.anim.baseLayer.activeState);
};```


Please help me out to solve this issue.

Hi @Haresh and welcome,

I think that’s expected since teleporting will override any momentum added to the object by forces/impulses, including grabity.

You may have to rework your player controller to use forces instead of calculating the final position yourself.

This example tutorial is a good start:
https://developer.playcanvas.com/en/tutorials/first-person-movement/