Cannot read property 'teleport' of undefined

i add this code. i hope if my player collider with movingbox, my player will teleport to the position

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

Movingbox.prototype.onCollisionStart = function (entity){
if(entity.other.tags.has("player")){
        console.log("hit");
        entity.rigidbody.teleport(0 ,1.49, -47.007);
    }
};

Hi @Dann,

Your code is correct you just need to make sure that the hit entity has a rigid body component. Make sure the other entity has a rigid body component in editor:

 Movingbox.prototype.onCollisionStart = function (entity){
if(entity.other.tags.has("player") && entity.other.rigidbody){
        console.log("hit");
        entity.rigidbody.teleport(0 ,1.49, -47.007);
    }
};

of course I added rigidbody, but it’s still not working

The collision callback returns a collision result, not an entity:

onCollision = function(result) {
    var entity = result.other;
    // entity.rigidbody
}

Your line under console.log is the issue.

Edit:
If you want to refer to the rigidbody of the entity that this script is attached to, instead of the one involved with the collision, you should use this.entity reference.

1 Like

it’s work, i found the problem is my rigidbody type is different. but my character can’t jump because it uses rigidbody kinematic. any solution? here’s my project https://playcanvas.com/editor/scene/1018463

I would recommend opening a new thread for a new question. It helps later to search the forum for solutions. In general, jumping mechanic depends on the game.

One option is to make your character a dynamic rigidbody. You can search the forum for jumping in that case - there were a few topics that can help you.

If you want to keep the kinematic body, then you must calculate the trajectory manually and translate the body along the calculated path. Given a starting point A and the highest jump point B, you can use trigonometry to find the landing point C by creating a parabola. You can then use a tweening function or a simple easing function to make the body start moving faster at the jump start, slowing at the top of the jump and then increasing again until it reaches the final point C.

You can find some tweening examples here:
https://playcanvas.github.io/#animation/tweening.html