Search Unity

First Person Footstep Sound

Discussion in 'Getting Started' started by abatisara98, Jul 5, 2020.

  1. abatisara98

    abatisara98

    Joined:
    Jul 25, 2019
    Posts:
    8
    Hi everyone,
    I'm having some trouble with my first person character. I would like to add the footstep sound when the player moves, but I don't know how to connect the public void Sound with the movement.
    My Player doesn't have a rigidbody, but just a character controller.
    Here's my script

    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     public CharacterController controller;
    4.  
    5.     public float speed;
    6.     public float gravity;
    7.     public float jumpHeight;
    8.  
    9.     public Transform groundCheck;
    10.     public float groundDistance;
    11.     public LayerMask groundMask;
    12.  
    13.     Vector3 velocity;
    14.     bool isGrounded;
    15.  
    16.  
    17.     public AudioSource Footstep;
    18.     public AudioClip[] FootstepSound;
    19.  
    20.     void Update()
    21.     {
    22.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    23.  
    24.         if (isGrounded && velocity.y < 0)
    25.         {
    26.             velocity.y = -2f;
    27.            
    28.         }
    29.  
    30.         velocity.y += gravity * Time.deltaTime;
    31.  
    32.         float x = Input.GetAxis("Horizontal");
    33.         float z = Input.GetAxis("Vertical");
    34.  
    35.         Vector3 move = transform.right * x + transform.forward * z;
    36.        
    37.  
    38.         controller.Move(move * speed * Time.deltaTime);
    39.  
    40.         //jump
    41.  
    42.         if(Input.GetKeyDown(KeyCode.Space) && isGrounded)
    43.         {
    44.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    45.  
    46.         }
    47.  
    48.  
    49.         controller.Move(velocity * Time.deltaTime);
    50.  
    51.         //run
    52.  
    53.         if (Input.GetKeyDown(KeyCode.LeftShift))
    54.         {
    55.             speed = speed * 2;
    56.        
    57.         }
    58.         else if (Input.GetKeyUp(KeyCode.LeftShift))
    59.         {
    60.             speed = speed / 2;  
    61.  
    62.         }
    63.        
    64.  
    65.     }
    66.  
    67.  
    68.     public void Sound()
    69.     {
    70.         int random = UnityEngine.Random.Range(0, FootstepSound.Length);
    71.         Footstep.PlayOneShot(FootstepSound[random]);
    72.  
    73.     }
    74.  
    75. }
    Thanks in advance!
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,537
    If you're using an Animator you can add callbacks in the animation at the point when the foot touches the ground. If you're not then it's just a matter of checking how much distance has been covered and play a sound if so.
     
    Joe-Censored likes this.
  3. abatisara98

    abatisara98

    Joined:
    Jul 25, 2019
    Posts:
    8
    No, I don't have an animation because it's a first-person game. So should I calculate my player's distance across space?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You could calculate the distance traveled each frame, then add it to a counter. When the counter passes whatever distance you want to play the sound, you reset the counter and play the sound.
     
    voncarp likes this.
  5. abatisara98

    abatisara98

    Joined:
    Jul 25, 2019
    Posts:
    8
    Thanks a lot, I'll try
     
    Joe-Censored likes this.