Search Unity

Question Play sound on collision

Discussion in 'Physics for ECS' started by argibaltzi, Feb 26, 2021.

  1. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Hello
    I am looking to add sounds when objects collide, i am trying to see what is the best way to do this.

    Are there any best practices?
    It seems that i would need to maintain an Enter/Stay/Exit state inside ICollisionEventsJob for every object that i need to handle sounds?
    Is there anything more optimal?

    How can i tell how hard an object hit a surface to see what sound to play?
    Is CollisionEvent.Details.EstimatedImpulse my best bet?

    Thank you!!
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @argibaltzi , you're right about everything and collision events are the intended solution for your use case. You can see how we did stateful events in UnityPhysicsSamples\Assets\Demos\2. Setup\2d. Events\2d2. Collision Events\Scripts\DynamicBufferCollisionEventAuthoring.cs . If you want to keep playing sounds as long as bodies are colliding, you can do it simply without the state.

    CollisionEvent.Details.EstimatedImpulse will tell you how "strong" the collision was.

    Please let me know if you have any further questions.
     
    jconsole and steveeHavok like this.
  3. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
  4. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    thank you gyes!