[SOLVED] Problems teleporting

Hi everyone,

I am trying to teleport the player to the position of another entity, but i cannot find the way to make it work
I am using a simple code:

var ChangeScene = pc.createScript('changeScene');

ChangeScene.attributes.add('sceneName', { type: 'string' });
ChangeScene.attributes.add("link1", { type: "string", title: "onclick Link" });
//
ChangeScene.attributes.add("cubemap", { type: "asset", title: "cubemap" });

ChangeScene.attributes.add('teleport', { type: 'entity' });
//


// initialize code called once per entity
ChangeScene.prototype.initialize = function () {
        this.entity.element.on('click', function () {
            this.loadMap();
        }, this);
    
};

ChangeScene.prototype.loadMap = function () {

    loadAtrezzo = this.app.root.findByTag('rig');
    loadAtrezzo.forEach(function (node) {
        node.rigidbody.teleport(this.teleport.rigidbody.position, pc.Vec3.ZERO);
    });

};

also tried using translate but nothing. The most mysterious thing is that it started giving me this error:

But that entity (teleport) is assigned:
imagen

So i do not know how to proceed

Hi @practicas_imas,

I think the first problem with your code is the lose of context, this isn’t what you expect at line 24. Try using an arrow function like this:

    loadAtrezzo.forEach((node) => {
       // now this refers to the script instance
    });

Later on try using the entity position directly:

    loadAtrezzo.forEach((node) => {
        node.rigidbody.teleport(this.teleport.getPosition(), pc.Vec3.ZERO);
    });
3 Likes

OMG it was jut that. Amazing

Thank you a lot!

1 Like