Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by benny279 · Oct 03, 2015 at 10:51 PM · c#footsteps

Footsteps via script

Hello,

I know this has been questioned many times as I've read majority of the posts regarding this, so I coded a footstep script in C# which works to some extent but the audio plays like crazy if you know what I mean? Example you walk on a certain texture on the terrain, it plays but very fast when walking but say you took a baby step at a time it plays fine which you would expect anyway. I have put a few checks in to stop it playing when the Rigidbody is sleeping or if the Character Controller is not grounded and vice versa.

Here's the script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioSource))]
 
 public class FootstepManager : MonoBehaviour
 {
 
     private AudioSource m_Audio;
     public AudioClip[] _GrassSounds;
     public AudioClip[] _MudSounds;
     public AudioClip[] _GravelSounds;
     public CharacterController _CharController;
     private Rigidbody _RigBod;
     private bool playSteps;
 
     void Awake()
     {
         m_Audio = gameObject.AddComponent<AudioSource>();
         _RigBod = _CharController.GetComponent<Rigidbody>();
     }
 
     void Start()
     {
         StartCoroutine(PlayFootstep());
         playSteps = true;
     }
 
     void Update()
     {
         if (!_CharController.isGrounded && playSteps)
         {
             playSteps = false;
             StopCoroutine(PlayFootstep());
         }
         else if (_CharController.isGrounded && !playSteps && _CharController.velocity.magnitude > 0.2f)
         {
             StartCoroutine(PlayFootstep());
         }
     }
 
 
     IEnumerator PlayFootstep()
     {
         int curSplatIndex = TerrainSurface.GetMainTexture(transform.position);
 
             switch (curSplatIndex)
             {
                 case 0:
                    yield return new WaitForSeconds(0.4f);
                    m_Audio.PlayOneShot(_GrassSounds[Random.Range(0, _GrassSounds.Length - 1)]);                   
                    break;
                 case 1:
                     m_Audio.PlayOneShot(_GravelSounds[Random.Range(0, _GravelSounds.Length - 1)]);
                     break;
                 case 2:
                     m_Audio.PlayOneShot(_MudSounds[Random.Range(0, _MudSounds.Length - 1)]);
                     break;
             }
         }
 
     }
 

I have tried many ways of trying to solve this but to no 'avail, hopefully someone can point me in the right direction!

Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by itsmars · Dec 23, 2015 at 03:45 AM

@benny279 I recorded a video tutorial on how to do this, and also included my script for free. Check it out, if you're still looking for a solution.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dexmes · Jul 04, 2018 at 05:58 AM 0
Share

Amazing video, the most advanced footstep script I have seen published yet I think! Thanks so much.

avatar image
1

Answer by MadDevil · Oct 05, 2015 at 06:18 AM

@benny279

if its an mecanin animation clip than you can use events for playing the footsteps sound.

http://answers.unity3d.com/questions/615041/mecanim-animation-events-1.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding a CharacterController makes my player move around the map involuntarily 0 Answers

Raycast and Footsteps not working; Standard Asset FirstPersonController and Viking Village,Getting Footsteps to Change Based on Material; FPSController Standard Asset & Viking Village 0 Answers

Unity noob here, need help adding footstep logic 1 Answer

C# Loop script,C# LOOP SCRIPT HELP NEEDED! 0 Answers

Detroy() deleting itself and another 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges