- Home /
How to add a footstep sound to rigidbody fps controller without animations?
My player has a rigidbody fps controller from standard assets. I don't have any animations or character. How can i add footsteps? I tried something like this. This doesn't work properly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FootStepController : MonoBehaviour
{
//config params
[SerializeField] GameObject Player;
//params
AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Player.GetComponent<Rigidbody>().velocity.magnitude > Mathf.Epsilon)
{
audioSource.Play();
}
else if (Player.GetComponent<Rigidbody>().velocity.magnitude == Mathf.Epsilon)
{
audioSource.Stop();
}
}
}
Answer by Llama_w_2Ls · Jul 25, 2020 at 05:53 PM
bool isWalking; //true if im walking
float FootstepDelayTime;
// Update is called once per frame
void Start()
{
StartCoroutine(PlayFootsteps()); //Starts the coroutine below (basically a function)
}
IEnumerator PlayFootsteps()
{
Start: //loop
if (isWalking == true) //check if im walking
{
audioSource.Play(); //play footstep sound (sound shouldnt really be longer than a second, just one footstep is enough)
yield return new WaitForSeconds(FootstepDelayTime); //delay for a period of time
}
goto Start; //loop back to checking if im still walking
}
Nice but how you control too isWalking? $$anonymous$$y Question is how can i understand character is walking without animation events?
If you press 'w' (to go forward) then isWalking is equal to true. You dont need animation events or triggers to detect if a key is being pressed.
Thanks for answers. If i tie isWalking to WASD how can i turn isWalking to false when i stuck somewhere but keep pressing WASD?
Answer by mzaidan1996 · Jul 24, 2020 at 10:25 AM
I'll try figuring it out, I'm new to unity, and I'm almost going to reach that step in my game, I'll try to find out how.
Your answer
![](https://koobas.hobune.stream/wayback/20220613003935im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there a way to make a wallrunning system using the character controller? 1 Answer
Rigidbody problem 0 Answers
How can i make my RigidBodyFPSController to move when Is Kinematic is true ? 1 Answer
Rigidbody controller counter movement 1 Answer
Camera shaking (problem with rigidbodyfpscontroller) 0 Answers