• 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 /
avatar image
0
Question by drp5205 · Jul 01, 2015 at 09:52 AM · audiofootstepsplayoneshot

PlayOneShot on footstep script either plays very delayed or not at all.

I'm trying to create my own footstep script to control the audio clip that played every given step length. Everything I've used to trace it seems like it should work. I'm getting "step" to print reliably in the console exactly how I'd like it, and the audio clips are randomizing well in my footstep AudioSource.

However, it just doesn't work. there's about a 10% chance that the audio will play at all, and when it does, there's a very low chance that it will play on time. Sometimes, it seems to cut off the sound, which I thought was impossible with playOneShot.

I'd really appreciate some help on this. My whole FootstepControl code is below. All arrays currently have a length of 8 and are filled with .wav files.

 #pragma strict
 
 public var footstepSource : AudioSource;
 
 public var watSteps : AudioClip[];
 public var mudSteps : AudioClip[];
 public var libSteps : AudioClip[];
 public var intSteps : AudioClip[];
 
 var currentClip : AudioClip;
 
 var n : int;
 
 var footstepSet : String;
 
 public var fpc : CharacterController;
 
 var distanceTravelled : float = 0;
 var lastPosition : Vector3;
 public var stepLength : float;
 
 var counter : int;
 
  function Start()
  {
       lastPosition = transform.position;
  }
  
  function Update()
  {
       counter++;
      
      distanceTravelled += Vector3.Distance(transform.position, lastPosition);
      
      print("frame " +counter + ": distanceTravelled1 " + distanceTravelled);
      
      if(distanceTravelled > stepLength && fpc.velocity.sqrMagnitude > 0){
              
              var p : int;
              print("step " + footstepSource.clip);
              footstepSource.PlayOneShot(footstepSource.clip, 1);
             playSound();
             changeSounds(footstepSet);
             
             //So same sound doesn't play twice in a row;
             p = Random.Range(0, 7);
              if( n != p){
                  n = p;
              }else {
                  n +=1;
              }
              
             distanceTravelled = 0;    
      }
      
      lastPosition = transform.position;
  }
  
  //Change Sound as per location, set by trigger volumes.
  function changeSounds(stepSet : String){
      
      if (stepSet == "water") {     
         currentClip = watSteps[n];         
      }
      if (stepSet == "library") {    
         currentClip = libSteps[n];         
      }
      if (stepSet == "intRoom") {     
         currentClip = intSteps[n];        
      }
      if (stepSet == "mud") {     
         currentClip = mudSteps[n];        
      } 
      
  }
  
  function playSound (){
      footstepSource.PlayOneShot(footstepSource.clip, 1);
  }
 

Any insight is appreciated! Thanks!

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Playing many audioclips with PlayOneShot causes all sound to cut out. 0 Answers

Sprinting Problem 0 Answers

How can i make my audio loop faster/slower? 0 Answers

Play multiple audio clips from one object 1 Answer

audio.Play() not always playing the entire AudioClip 5 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges