sound only plays when moving forward

i wonder if someone can help me?Ii have movement/sound script in my FPS connected to my player the idea is that he should have a footstep sound for moving fowerd & back and left & right however my audo only plays when the player moves fowed can anyone hlep me with this? here is what I have so far

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



public class Movement : MonoBehaviour {
    public float Speed = 2f;
    public float Senctivaty = 2f;
    public GameObject Eyes;
    public float jumpforce = 4f;
    CharacterController player;
    float MoveFB, MoveLR, RotX, RotY, vertVelocity;
    private bool HasJumped,isCrouched;
    public AudioSource Audios;
    public AudioSource Audios2;
    // Use this for initialization

    void Start() {
        player = GetComponent<CharacterController>();
       
    }

    // Update is called once per frame
    void Update()
    {
       
        MoveFB = Input.GetAxis("Vertical") * Speed;
        MoveLR = Input.GetAxis("Horizontal") * Speed;
        RotX = Input.GetAxis("Mouse X") * Senctivaty;
        RotY = Input.GetAxis("Mouse Y") * Senctivaty;

        transform.Rotate(0, RotX, 0);
        Eyes.transform.Rotate(-RotY, 0, 0);

        Vector3 movement = new Vector3(MoveLR, vertVelocity, MoveFB);
        movement = transform.rotation * movement;
        player.Move(movement * Time.deltaTime);
        PlayFootsteps();
        Playfootsteps2();




        if (Input.GetKeyDown(KeyCode.Space))
        {

            HasJumped = true;
        }
        if (Input.GetButtonDown("Crouch"))
        {
            if (isCrouched == false)
            {
                player.height = player.height / 2;
                isCrouched = true;
            }
            else
            {
                player.height = player.height * 2;
                isCrouched = false;
            }
        }
        
      ApplyGravty();
}



    private void ApplyGravty()
    {
        if (player.isGrounded == true)
        {
            if (HasJumped == false)
            {
                vertVelocity = Physics.gravity.y;
               
            }
            else
            {
                vertVelocity = jumpforce;
            }

        }
        else
        {
            vertVelocity += Physics.gravity.y * Time.deltaTime;
            vertVelocity = Mathf.Clamp(vertVelocity, -50f, jumpforce);
            HasJumped = false;

        }
  
    }

    private void PlayFootsteps()
    {
        if (MoveFB > 0.1f && MoveFB < Speed + 0.1f)
        {
            Audios.enabled = true;
            Audios.loop = true;
        }
        if (MoveFB < 0.1f)
        {
            Audios.enabled = false;
            Audios.loop = false;
        }
        Playfootsteps2();
    }

    private void Playfootsteps2()
    {
        if (MoveLR > 0.1f && MoveLR < Speed + 0.1f)
        {
            Audios2.enabled = true;
            Audios2.loop = true;
        }
        if (MoveLR < 0.1f)
        {
            Audios2.enabled = false;
            Audios2.loop = false;
        }
    }
}

Thank you for your help :slight_smile:

Assuming your audio clip doesn’t last at least 1 millisecond, I don’t think it’s a good idea to call your audio playing methods in Update (because it only lasts for about a millisecond or less).

A good thing to do is to add a timeout to your script so there’s space between each time the sound is being played (so it can play).

Also calling both your foot step methods at once will result in the first one not being played at all, you should also add Random.Range(0, 2); for some variation in sound.

add something like this to your script and It’ll work


int audioTimeout;

void Awake(){
    audioTimeout = 0;
}

void Update(){
    if(audioTimeout == 0){
        int audioRan = Random.Range(0, 2);
        if(audioRan == 1){
            PlayFootStep1();
        }else{
            PlayFootStep2();
        }
        //adjust this value to decide the space between the sound
        audioTimeout = 25;
    }else{
        audioTimeout--;
    }
}

PlayFootStep1(){

}

PlayFootStep2(){

}

I hope this helps! I’ll be around if you need me