1
\$\begingroup\$

The animation in the Animator is HumanoidWalk In the inspector of the animation in the Animation tab at the bottom I clicked on the Events clicked to add event names the function as Step and moved the time slider to 0.67 moved the slider in the preview window of the character so the right leg foot will be close to the ground and clicked on apply:

Foot1

Then I did the same but changed the time a bit more almost to 0.7 and called the function Step again and at the preview window now it';s the other leg foot close to the ground and clicked apply:

Foot2

I added to the character in the hierarchy a Audio Source component and a script name Foot Steps:

Foot3

And the script it self:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FootSteps : MonoBehaviour
{
    [SerializeField]
    private AudioClip[] clips;

    private AudioSource audioSource;
    private GameObject[] Foots;

    private void Awake()
    {
        audioSource = GetComponent<AudioSource>();
        Foots = GameObject.FindGameObjectsWithTag("Foot");
    }

    private void Step()
    {
        AudioClip clip = GetRandomClip();
        audioSource.PlayOneShot(clip);
    }

    private AudioClip GetRandomClip()
    {
        return clips[UnityEngine.Random.Range(0, clips.Length)];
    }
}

In the Hierarchy in the script in Clips I added same 8 audio clips just for the testing I could add only 1 it doesn't matter:

Foot4

When running the game I marked with blue circle only this foot play the step/s sounds. And if I change the time to be the same 0.67 it will play two sound the same so it will sound like one or if the time is 0.67 and 0.7 it will hear two steps but only on the left leg foot.

Foot5

The second leg foot never play the sound no matter how many times I changed the preview animation legs.

I tried to follow the instructions of this tutorial but no success:

Add foot steps tutorial

Should be simple but only one foot is working. I can't figure out what am I missing.

\$\endgroup\$
1
\$\begingroup\$

I have had trouble calling events on the animator. This doesn't answer your specific question but provides another solution. The way I do footstep sounds is to raycast from the foot bone and play the footstep sound effect that corresponds to the surface which the raycast collides with. You can set the surface type several ways. I use layer mask since it is easily integrated with Physics.Raycast(). Physics.Raycast reference

private void PlayFootStepSound(){
    RaycastHit hit;

    if(Physics.Raycast(footBone.transfrom.position, 
    -footBone.tranform.up, out hit, stepDistance, surfaceLayer){

        // make the magic happen....
    }
}

Hope this helps.

\$\endgroup\$
0
\$\begingroup\$

"You're adding your events at the wrong timestamps. Take a look at the little white line in the "Events" timeline. In your first screenshot, the line is right past 0:33 and in your second screenshot, the line is slightly right of 0:83. That's where your events should be (not 0:67 and 0:70)."

It was my wrong thought that I need to set the timestamp on my own. Instead all I needed to do is to set in the preview window each time the step of the character for each foot and when clicking on the add event button it's setting the timestamp automatic. And I was setting it on my own. This part was not explained in the video tutorial I followed:

Add footsteps tutorial

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.