[SOLVED] How To Use 'collisionend' Correctly?

I know this sounds like silly issue, but I want my player to rotate back to its original rotation. The problem is I tried using result.other.tags.has() and result.other.tags.had() but there was an error saying that tags is not found.

Here’s My 'collisionend' code:

PlayerController.prototype.onCollisionEnd = function(result) {
    if (result.other.tags.had('turn45') || result.other.tags.had('turn90')) {
        this.main.setLocalEulerAngles(0, 0, 0);
    }  
};

Link To Editor: https://playcanvas.com/editor/scene/898825

There is no had() method on the pc.Tags class, only has().

Also you need to check if result isn’t equal to null, otherwise your code will throw an exception and stop executing.

PlayerController.prototype.onCollisionEnd = function(result) {
    if (result && (result.other.tags.has('turn45') || result.other.tags.has('turn90'))) {
        this.main.setLocalEulerAngles(0, 0, 0);
    }  
};

I want it so that when you leave a platform that’s turned 45 degrees or 90 degrees, the player will reset to 0 euler angles.

Also, I still get the same error:

Check before line 275 if result or result.other are defined or not.

Otherwise it’s normal to be getting that error from time to time, you need to guard your check like that.

result has been defined as a parameter. It works just fine in ‘collisionstart’ or ‘contact’. It doesn’t seem to work in ‘collisionend’.

I know there is something different with collisionend. I had the same issue but i forget what the solution is. Maybe try it without other.

According to the docs, collisionend will provide the other entity directly, not as a property of result:

PlayerController.prototype.onCollisionEnd = function(other) {
    if (other && (other.tags.has('turn45') || other.tags.has('turn90'))) {
        this.main.setLocalEulerAngles(0, 0, 0);
    }  
};

https://developer.playcanvas.com/en/api/pc.CollisionComponent.html#event:collisionend

1 Like

Ah I was close! :yum:

1 Like

Indeed, you reminded me to take a look at the docs again :slight_smile:

1 Like

Thank you and @albertos for finding out the problem. The code works perfectly. I will now mark this as solved.

2 Likes