Help to solve this teleport code

if i add this teleport is working but getPosition is not defined

var Movingbox = pc.createScript('movingbox');

Movingbox.prototype.initialize = function() {
    this.entity.collision.on("collisionstart", this.onCollisionStart, this);
};

Movingbox.prototype.onCollisionStart = function (entity){
    if (entity.other.tags.has("player")) {
        var test = this.app.root.findByName("test").getPosition();
        entity.other.setPosition(test);
        console.log("hit");
        getPosition(test);
   }
};

and if i delete getPosition(test) the teleport is not working or i change to test(); its working but it will say another error.

If you get an error saying getPosition is not defined, then findByName is not finding an entity in your scene called test. Double check the naming. For example, did you perhaps name it Test?

1 Like

Hi @Dann! On which line of your code do you get the error? I’m not sure what you are doing with the last line of the onCollisionStart function:

    getPosition(test);
1 Like

it isn’t “entity” instead its “result”. also remove the getPosition(test) if it isn’t a custom function and it should work.

var Movingbox = pc.createScript('movingbox');

Movingbox.prototype.initialize = function() {
    this.entity.collision.on("collisionstart", this.onCollisionStart, this);
};

Movingbox.prototype.onCollisionStart = function (result){
    if (result.other.tags.has("player")) {
        var test = this.app.root.findByName("test").getPosition();
        result.other.setPosition(test);
        console.log("hit");
       // getPosition(test);
   }
};
1 Like

if I delete the last line it won’t work. and if I change it with the entity name (test) the error will change to not a function

It’s not possible for me to debug this problem without seeing your project.

Here’s my project https://playcanvas.com/editor/scene/1018463

Hello @Dann! I’ve looked at your project and as far as I can tell it’s almost okay. I don’t think you can use setPosition() in this case. You have to use teleport() if you are using a dynamic rigidbody. Also test() is not a function in your project so you have to remove this line or create a function with this name.

Try replacing this code:

   else if(entity.other.tags.has("player")){
        var test = this.app.root.findByName("test").getPosition();
        entity.other.setPosition(test);
        console.log("hit");
        test();
   }

with this:

   else if (entity.other.tags.has("player")) {
        var test = this.app.root.findByName("test").getPosition();
        entity.other.rigidbody.teleport(test);
        console.log("hit");
   }

i think it’s not working with rigidbody dynamic collider with rigidbody kinematic.

but if i do this it’s working when dynamic collider with kinematic

 else if(entity.other.tags.has("player")){
        var test = this.app.root.findByName("test").getPosition();
        entity.other.setPosition(test);
        console.log("hit");
        test();
   }

So if I understand you correctly the problem is solved?

yes I hope this problem can be resolved :slightly_smiling_face:

Is the problem solved or not?

it is solved if my character rigidbody type is static or kinematic, but if I change my rigidbody character type to static or kinematic the jump function doesn’t work, because the jump function requires a dynamic rigidbody type

You have to determine what is the best option for your game. A player with a kinematic rigidbody or a player with a dynamic rigidbody. Then you have (re)make the others functions, like your jump function, so that everything is working for that type of rigidbody.

If you keep the player with the dynamic rigidbody body, did you already try the code that I sent to you?

of course i try the code you give it to me

if it work my character will not stand on the trap.

i tried to change the trap rigidbody to dynamic and my character to kinematic it works

But I assume this is not what you want as it will broke your other functions? If you add me to the project with write access it’s easier for me to debug why it’s not working with the player that has a dynamic rigidbody.

You can fork my project

anyone help please i dont know this is bug or not. why my character with rigidbody dynamic collider with entity rigidbody kinematic it can’t teleport. i trying to change my character rigidbody to kinematic and my entity to dynamic teleport is work If I do that, my character can teleport, but this is not what I want

It took a while, but I found out that the update function of your playerMovement script hinders teleporting. I therefore added an extra boolean and set it just before teleporting. With this boolean I block the update function of your playerMovement script. Then it works as expected. The reason why it also works after an error is because the error also stopped the update function of your playerMovement script.

so how to fix it?