Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Footstep sounds and blending

Discussion in 'Animation' started by joshcamas, Oct 6, 2019.

Thread Status:
Not open for further replies.
  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Hello!

    I'm having some issues with getting footstep sounds to work alongside blending animations.
    I have 3 animations: idle, walk, run. And there's a blend tree that blends these animations depending on player speed. Awesome!

    BUT.

    Each animation has a AnimationEvent that triggers the "OnFootstep" event whenever the character's feet touch the ground. When blending in between run and walk, this means its playing way too many sounds, since both run footstep events and walk footstep events are being triggered.

    Any thoughts on fixing this? One idea is to require that the weight of the animation triggering it must be at least .5, but this means the sounds wouldn't match correctly with the footsteps.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    We never use animation events for footsteps, for that exact reason. Instead, we measure the y-position of the foot bones, and play a sound when they fall below a certain threshold.
     
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Ahh interesting. That definitely sounds like something I could do! Thank you!
    I really hope Unity considers adding something like animation events but ones that are blend friendly, because mixing the two often is impossible :S I have no idea how that would work, however. It would probably require that the clips being blended have the same number of events. Then, that would probably be possible.
     
    Last edited: Oct 7, 2019
  4. CARRASC

    CARRASC

    Joined:
    Apr 19, 2018
    Posts:
    3
    I know this post is old, but for anyone that still has this issue in the future, I think you can use the weight property of the event and only play the sound when that weight is higher than a threshhold. This seemed to work out for me and hopefully it can help someone in the future:

    Code (CSharp):
    1. public void Step(AnimationEvent evt)
    2.     {
    3.  
    4.         if (evt.animatorClipInfo.weight > 0.5f)
    5.         {
    6.             source.PlayOneShot(GetRandomClip());
    7.         }
    8.     }
     
  5. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,554
    Note that taking an AnimationEvent parameter will allocate a bunch of garbage every time the event is triggered.
     
  6. CARRASC

    CARRASC

    Joined:
    Apr 19, 2018
    Posts:
    3
    Really?? Would you say that this method is no good then? I will try the other way as well then and see how it goes
     
  7. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,554
    It works and there's a reasonable chance that it won't cause noticeable performance issues, but it's obviously not ideal.

    I made my own event system for Animancer which can apply events to a whole mixer (equivalent to a Blend Tree) rather than the individual animations, but for something like this I'd probably go for the Y position solution suggested by Baste anyway. Especially if you're already using IK to adjust the foot positioning on uneven ground since that would give physically realistic results without needing to manually set up events on all your animations.
     
    andreiagmu and CARRASC like this.
  8. Lucas_Lundmark

    Lucas_Lundmark

    Joined:
    May 18, 2017
    Posts:
    5

    If anyone stumbles upon this thread, this worked for me:
     
    Fressbrett likes this.
  9. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Yeah, this is what I used to make things work! Essentially what I did was for each animation (walk, run, sprint) add an integer value (1 = walk, 2 = run, 3 = sprint). Then when the footstep event triggers, check the integer and match it to the character's speed, deciding which integer value is "valid". If the event has a valid integer value, play the sound! Otherwise ignore it.
     
  10. Sipel

    Sipel

    Joined:
    Jan 13, 2015
    Posts:
    4
    @Kybernetik (I tag you to, cause you seem to know what he was talking about xD)
    I was looking at this suggestion to implement footsteps in my project, but I have some doubts about my implementation. Right now I've attached a monobehaviour script to the foot of my character in the rig hierarchy, and I measure the localPosition of that transform to get when the foot changes his y direction and get the timing right. This is working right now but gives me some constraints that I don't know how to manage without injecting more info from a higher level script:
    • What if I'm doing a skill, like a fast charge where the foot doesn't touch the ground and a skill, like a simple sword slash where my character put one foot forward. I want to sonorize the latter, but not the first, so should I use some threshold system to differentiate the cases or there is a smarter solution? Cause if I use a threshold system then I need to configure it for every different character (I don't have humanoid characters, they are animal-like ones), and could not work for every skill.
    • If I have an IK system, using threshold seems risky to me cause you never know the effective delta y that is right for all cases, it seems to me that I need more conditions to reach good results.
    Right now I'm a bit confused, but it really seems the smarter and fastest way to implement footsteps so I was wondering if you could give me some more intel about conditions and maths that you would use to implement this.
     
  11. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,554
    I've never actually done anything like this so I'm just theory-crafting here, but in Animancer I'd start by creating a custom transition type with a bool to determine whether footstep sounds should be enabled during that animation or not (like the MotionTransition class in the Root Motion example). I'm not sure if there's a good way to add metadata to an animation like that with Animator Controllers, maybe something with StateMachineBehaviours.

    Using this approach with IK seems like it should be even easier to set up because you don't need some arbitrary height threshold, you can actually detect when the foot touches the ground. Or since you're likely using an animation curve to control the IK weight, simply triggering a footstep sound whenever the weight reaches 1 should work reasonably well too.
     
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    For animations that are not blended - like a step forwards for an attack - you can use the event approach. Then for the blended walk/run animation, and other blended animations that need footsteps, you use the y-position based approach.

    If you're doing IK, you might want to instead tie it into your IK solution, as you'll have better ideas about when the foot is grounded or not.
     
    John_island and EpicMcDude like this.
  13. JamesHPak

    JamesHPak

    Joined:
    Apr 15, 2021
    Posts:
    1
    Wish Unity could blend these events and give us a single event at correct frame instead of firing multiple events. Seems doable.
     
    John_island, clownhunter and Alpants like this.
  14. KalOBrien

    KalOBrien

    Administrator

    Joined:
    Apr 20, 2021
    Posts:
    89
    Locking this thread as a solution has been found.
     
Thread Status:
Not open for further replies.