Teleport Player to a position

Hello, I have a problem. I want to get the player to teleport to a certain area (that I haven’t made yet). I don’t really know how to code at all. Could somebody tell me how I could do it?

Hi @AmericanHero999,

Take a look at this tutorial, it explains both how to setup triggers and detect/teleport any object that’s stepping on it:

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

Where would I put in the position of where the player teleports to? I have the rigidbody and collision on the teleport area.

var Trigger = pc.createScript('trigger');

// initialize code called once per entity
Trigger.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

Trigger.prototype.onTriggerEnter = function(entity) {
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // Reset back to roughly the position the entity started in.
    var position = entity.getPosition();
    entity.rigidbody.teleport(position.x, 10, 0);
};

Suppose if you want to teleport your entity to (10,10,10) position you can just write a line and it will work.
entity.rigidbody.teleport(new pc.Vec3(10,10,10));

I inserted that into the code, and I made the player collide into it, nothing happened. I was also using https://developer.playcanvas.com/en/tutorials/first-person-movement/ 's code.

var Trigger = pc.createScript('trigger');

// initialize code called once per entity
Trigger.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

Trigger.prototype.onTriggerEnter = function(Entity) {
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // Reset back to roughly the position the entity started in.
    var position = entity.getPosition();
    entity.rigidbody.teleport(position.x, 10, 0);
    entity.rigidbody.teleport(new pc.Vec3(10,10,10));
};

Could you link your project please @AmericanHero999?

Yeah, sure. https://playcanvas.com/project/705199/overview/firstpersontestdownstairs-demo

1 Like

Thanks. I’ll take a look today, and revert.

Ok, this is quite a large project. Could you try to isolate the error in a smaller project? Alternatively, steps on how to reproduce the error in this one?