Search Unity

Question FootStep Audio Problem

Discussion in '2D' started by XsvGamer, Jul 3, 2020.

  1. XsvGamer

    XsvGamer

    Joined:
    Apr 9, 2020
    Posts:
    20
    Hi guys,

    I've got a problem with my 2D Sidescroller player footstep sound.

    I've basically added an AudioSource to an empty object on the hierachy as a child of the Player and named it Footsteps. I've added the audio file to this variable I created called AudioS which is in the PlayerController Script which is obviously attached to the player. I have attached the footstep empty object containing the audio source into AudioS within the inspector for the Player within Unity. I have also enabled the function within UPDATE

    //FOOTSTEPS FUNCTION
    private void PlayFootSteps()
    {
    if(theRB.velocity.x >0.1 && isGrounded)
    {
    AudioS.enabled = true;
    AudioS.loop = true;
    }
    if (theRB.velocity.x <-0.1 && isGrounded)
    {
    AudioS.enabled = true;
    AudioS.loop = true;
    }else
    {
    AudioS.enabled = false;
    AudioS.loop = false;
    }
    }

    MY PROBLEM:
    Ok, so this seems to work well when the character is moving to the left, it cuts out when moving left and jumping so everything in that direction seems to work perfectly, however, when going right, nothing works?? Any ideas?
     
    Last edited: Jul 3, 2020
  2. XsvGamer

    XsvGamer

    Joined:
    Apr 9, 2020
    Posts:
    20
    Ok, figure it out, and seems to work a charm now... I know i'm talking to myself here right now...lol - but at least I'll post my solution here incase anyone else has any trouble with this in the future...(basically, needed to know how to implement OR in an if statement... || )

    //FOOTSTEPS FUNCTION
    private void PlayFootSteps()
    {
    if(theRB.velocity.x >0.1 && isGrounded || theRB.velocity.x < -0.1 && isGrounded)
    {
    AudioS.enabled = true;
    AudioS.loop = true;
    }
    else
    {
    AudioS.enabled = false;
    AudioS.loop = false;
    }
    }
     
  3. XsvGamer

    XsvGamer

    Joined:
    Apr 9, 2020
    Posts:
    20
    **EDIT - Got a bug**

    I have come across a problem with this though, whenever the player is right next to another rigidbody/collider the sound will play over and over but at a faster pace...any ideas??
     
  4. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    With just that Method i can't know whats happening. But seems like your AudioSource is enabling and disabling constantly. If you have "PlayOnAwake" actived, each time you enable the AudioSource it will sound...

    1- Could be the collider at your right interacting with isGrounded? Are you sure only the ground is able to disable this bool?

    2- You could try also instead of AudioS.enabled = true / false; to do AudioS.Play() / Stop() and remove the playOnAwake at inspector or at your script Awake() method.

    3- Apart from this issue, I'm pretty sure you dont need to set AudioS.loop each time you enable or disable it, Just set it as true by default.

    PD: Vars like AudioS (assuming is not a get variable) used to be written with lowercase at 1st letter.