- Home /
 
 
               Question by 
               ItArth · Jan 24, 2020 at 04:29 PM · 
                scripting problemcharacter controllerground detection  
              
 
              Check if player is grounded and play a sound 1 time
How i can make that detect if the player is grounded and play the footstep audio 1 time? because it is repeating the sound all the time when i am grounded
 if (m_CharacterController.isGrounded)
 {
        FootstepSounds footstep;
        footstep = GetComponentInChildren<FootstepSounds>();
        footstep.FootStep(); //PLAY THE SOUND
 }
 
              
               Comment
              
 
               
              Answer by lgarczyn · Jan 25, 2020 at 01:27 AM
Lots of way to do it, like using OnCollisionEnter and checking the tag of the other object, but you can try this: Declare a variable to store the previous state
 private bool wasGrounded = true;
 
               And then just do
  if (m_CharacterController.isGrounded && !wasGrounded)
  {
     GetComponentInChildren<FootstepSounds>().FootStep(); //PLAY THE SOUND
  }
   wasGrounded = m_CharacterController.isGrounded;
 
              Your answer
 
             Follow this Question
Related Questions
Help with crouch script! 2 Answers
Look On Cursor Problem 1 Answer