I am working on a game in which there is an opportunity to jump. When a player lands, I want to reproduce the sound of a fall. I tried to use the contact event for collisions, but it sometimes does not work or works late. Tell me, please, how to do it correctly? Thanks.
I just recently coded the logic for this on a FPS game I’m developing, you can see it working:
What I do is simply raycast down and define a bool to check if the player is grounded or not, then when you jump you set the variable to ‘false’, and in the raycast check if it was false, then play the sound and set the variable to ‘true’ again.
The entity of the player has a rigid body component and a collision. The player also has a mass. When falling, he jumps up a little and then lands back. You are most likely right that many calls occur upon landing and the game engine does not have time to process them all. To solve this problem, I need to set the restitution parameter to zero in the settings of the physics engine?
Thanks for the advice, but in my project that will not work correctly, because I have a third-person shooter in which jumping is one of the main mechanics. If I do a raycast, it will only work when there is a surface under the player. If the player lands on the edge, the sound event will not work, because the beam will not hit the surface.
I solved the problem as follows. I set the restitution parameter to zero, add variable that stores the state of the ability of the jump and used the event collisionstart, which works one time and causes the sound to play.