Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sound to walk

Discussion in 'Audio & Video' started by bertus1987, Aug 27, 2023.

  1. bertus1987

    bertus1987

    Joined:
    Jul 23, 2022
    Posts:
    5
    Hi,
    I want to know how i can put the sound in loop while the player is walking. I use for example Input.GetKey(KeyCode.RightArrow) but if i'm pressing the right key the sound only play once. Can you help me?
    Thank you
    Alberto
     
  2. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    ChatGPT

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(AudioSource))]
    4. public class PlayerWalkSound : MonoBehaviour
    5. {
    6.     private AudioSource audioSource;
    7.     private bool isWalking = false;
    8.  
    9.     // Unity's predefined keycodes for basic movement
    10.     private KeyCode[] walkKeys =
    11.     {
    12.         KeyCode.W,
    13.         KeyCode.A,
    14.         KeyCode.S,
    15.         KeyCode.D,
    16.         KeyCode.UpArrow,
    17.         KeyCode.LeftArrow,
    18.         KeyCode.DownArrow,
    19.         KeyCode.RightArrow
    20.     };
    21.  
    22.     private void Awake()
    23.     {
    24.         audioSource = GetComponent<AudioSource>();
    25.         audioSource.loop = true; // Make sure the sound loops
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         bool currentWalkingState = IsWalkingKeyActive();
    31.  
    32.         // If state changed from not walking to walking, play sound
    33.         if (currentWalkingState && !isWalking)
    34.         {
    35.             audioSource.Play();
    36.         }
    37.         // If state changed from walking to not walking, stop sound
    38.         else if (!currentWalkingState && isWalking)
    39.         {
    40.             audioSource.Stop();
    41.         }
    42.  
    43.         isWalking = currentWalkingState;
    44.     }
    45.  
    46.     private bool IsWalkingKeyActive()
    47.     {
    48.         foreach (KeyCode key in walkKeys)
    49.         {
    50.             if (Input.GetKey(key))
    51.             {
    52.                 return true;
    53.             }
    54.         }
    55.  
    56.         return false;
    57.     }
    58. }
     
    bertus1987 and Ryiah like this.
  3. marteko

    marteko

    Joined:
    Apr 4, 2016
    Posts:
    51
    I think the 2nd tutorial named "Create dynamic sound effects" from the Audio mission of Creative Core leaning section is exactly what you need.
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    Attach sound source to the character. It has property "looping" or similar one somewhere. Set it to true. Begin playing when player moves, stop playing when it stops. That's it.

    Note that in this case step sounds will probably mismatch step times when character puts foot on the ground. Depending on the genre it might not matter and might not be noticeable.
     
    bertus1987 likes this.
  5. bertus1987

    bertus1987

    Joined:
    Jul 23, 2022
    Posts:
    5
    Thank you