Search code examples
collision-detectionunity-game-engineunity3d-2dtools

How can I detect when my moving ball hits non-moving brick?


I have been stuck on this for awhile now. I want to produce the sound of a bouncing ball when my ball falls onto the brick, so obviously I don't want the ball to go through the brick. However I want the brick to be completely static. How can I detect when the ball bounces on the brick without moving the brick? Currently I was playing around and got stumped. At my current settings, the sound is produced like I want, however since it is set to "is Trigger" the ball just passes through the brick, which I don't want it to do. I want it to bounce on the brick without affecting the brick's position at all (static). Please help:

Inspector


Solution

  • Uncheck the Is Trigger bool on the collider. Then add a custom script to the ball which overrides OnCollisionEnter. Follow the link for sample code.

    I hope that helps!