Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Collision detection with sphere collider against curved mesh wall collider -- and bounce sounds

Discussion in 'Physics' started by DivideByNil, Mar 30, 2016.

  1. DivideByNil

    DivideByNil

    Joined:
    Dec 4, 2015
    Posts:
    15
    I'm trying to play a bounce sound effect when a sphere collides with a wall provided the sphere has a great enough velocity magnitude, and that works fine as long as the wall is flat.

    However, I have an issue when the sphere rolls against a curved wall with a mesh collider. The sphere rapidly triggers OnCollisiionEnter() and OnCollisionExit() functions as it rolls against the curved wall mesh collider. Since the sphere is technically moving fast enough to bounce, the bounce sound plays when rolling along a curved surface -- even though no visible bounce is taking place.

    So, I have a couple questions:

    1) Is there a way to actually get the magnitude or velocity of a bounce? So, for example, if the sphere is rolling along a curved wall, it's not visibly bouncing. Can I detect that and therefore not play the bounce sound?

    2) What method should I use to trigger a bounce sound? Right now I'm detecting the sphere magnitude against the mesh wall collider with OnCollisionEnter(), and if the velocity is high enough the bounce sound plays. As I said, that works fine on flat walls, but not curved walls since the sphere can be traveling fast but not actually bounce.

    That's it! Can anyone help? Thanks a lot! :)
     
    Last edited: Mar 30, 2016
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I think you have a good method, but you should be checking the size of the component of the velocity in the direction of the normal vector instead of just the magnitude of the velocity.

    An easier way is to just see how much force the physics engine is applying on the bounce. Just check the magnitude of Collision.impulse on the Collision returned by OnCollisionEnter(), and play the bounce sound if it's over a threshold.
     
    DivideByNil likes this.
  3. DivideByNil

    DivideByNil

    Joined:
    Dec 4, 2015
    Posts:
    15
    I really appreciate your response, Hyblademin. It looks like Collision.impulse is exactly what I need.
     
    Last edited: Mar 31, 2016