Play sound only on collision with certain object

I have a scene with SpongeBob, a basketball, and a player. SpongeBob says “Hi! i’m SpongeBob!” when I touch him as a player, but also plays the noise when the basketball touches him. This gets annoying fast, is there any way I could make it so SpongeBob only plays the noise when the player hits him?
Here’s my code:

var Collider = pc.createScript('collider');

// initialize code called once per entity
Collider.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

Collider.prototype.onCollisionStart = function (result) {
    if (result.other.rigidbody) {
        this.entity.sound.play("hit");
    }
};

Hi @Aksel_Walztoni!
You can acess the entity you hit’s whole data, including name, if you add
&& result.other.name === "Name"
or
&& result.other.tags.has("someTag")
to filter just for that tag.

1 Like