[SOLVED] Entity returns to initial position after using setPosition

Hi.

I’m using two methods of navigation.
One it’s setting the postion of the camera with a menu and the you’re transleted to the desired position.
The second one it’s the first person movement controller example.

The issue it’s after using the teletransportation and then moving with first person movement the entity returns to the initial position there’s a way to store the last postion?

Project Link

First person movement

FirstPersonMovement.prototype.update = function(dt) {
    // If a camera isn't assigned from the Editor, create one
    if (!this.camera) {
        this._createCamera();
    }

    var force = this.force;
    var app = this.app;

    // Get camera directions to determine movement directions
    var forward = this.camera.forward;
    var right = this.camera.right;


    // movement
    var x = 0;
    var z = 0;

    // Use W-A-S-D keys to move player
    // Check for key presses
    if (app.keyboard.isPressed(pc.KEY_A) || this.virtualSticks.script.virtualSticks.leftX > 0) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D) || this.virtualSticks.script.virtualSticks.leftX < 0) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W) || this.virtualSticks.script.virtualSticks.leftY > 0) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S) || this.virtualSticks.script.virtualSticks.leftY < 0) {
        x -= forward.x;
        z -= forward.z;
    }

    // use direction from keypresses to apply a force to the character
    if (x !== 0 && z !== 0) {
        force.set(x, 0, z).normalize().scale(this.power);
        this.entity.rigidbody.applyForce(force);
    }

    // update camera angle from mouse events
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
    
    var en = this.entity.getPosition();
    //var rb = this.entity.rigidbody.getPosition();
    
    console.log("Posicion entidad", en);
    //console.log("Posicion rigib-body", rb);
};

Setting the position to another entity

this.Player.setPosition(
                this.EntidadInformaciones.children[i].position.x,
                this.EntidadInformaciones.children[i].position.y,
                this.EntidadInformaciones.children[i].position.z);

Ah I solved it the problem was I needed to teleport the rigid body

this.Player.rigidbody.teleport(position, pc.Vec3.ZERO);

Can you please mark it as “Solved” Thanks :smiley:

2 Likes