[SOLVED] Play a certain sound when touching entity

How would I make it so if I am touching a certain entity a certain sound plays?

never mind I figured it out.

Iā€™m proud of you! :muscle:

Can you share how you did this, to help others with the same question?

Thanks!

var Sound1 = pc.createScript('sound1');

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

Sound1.prototype.onCollisionStart = function (result) {
    if (result.other.name === 'House2') {
        this.entity.sound.play('House');
        this.entity.sound.stop('Night');
    }
    else{
        this.entity.sound.play('Night');
        this.entity.sound.stop('House');
    }
};

this is what I did

2 Likes

There is also a tutorial here that shows exactly that: Collision and Triggers | Learn PlayCanvas

1 Like