FPS how to make a sound play when collision from another object hits an object

https://playcanvas.com/editor/scene/1599119

i’m new and just doing this for class, is there anyone who can help me find a solution.
so i have a fps game, and i need to play a different sound per object when it collides with the bullet.
can someone teach me how to do this? thanks

@migs1 Welcome! This forum and its history are the place to find answers to your questions. The question you are asking probably has answer by searching the forum. Here is a quick search.

Search results for ‘play a sound on collision’ - PlayCanvas Discussion

Also, to help you with actions and collisions you could have a look at this tutorial section.
Tutorials | Learn PlayCanvas

Lastly you could also take a look at this project.
Overview | Dashboard | WeaponTest | PlayCanvas | 3D HTML5 & WebGL Game Engine

1 Like

i’m trying to figure out how to make different objects play sound when bullet hits it

@migs1 I am not sure what method you would be using to fire the bullet at the item but if the rigidbody of a physical bullet collides with the object then you could use the method below in this example.

// Enter trigger
ItemPickup.prototype.onTriggerEnter = function(object) {

    if(object.tags.has('player')) {

        // play the pickup sound
        if(this.entity.sound) this.entity.sound.play("Pickup");
              
        // increase the munitions amount
        console.log("pickup");

        // Increase item amount
        object.fire("player:addhealth",this.amount);

        // Disable after Pickup
        this.entity.enabled = false;
           
    }

};

This script basically play the sound attached to the object when the player runs over top of it. One thing you will have to do is attach the sound like this.

If you are raycasting to the object you will need to fire an event in the code for the other object like this example:

    if(result) {

        console.log("You Hit: " + result.entity.name);
        this.handleImpact(result.entity,result.point,result.normal);
        // Fire a hit event on object
        result.entity.fire("hitTarget",this.player);
 
    } else {

        console.log("Nothing Hit");
    }

Put something like this in your raycast handling.

1 Like