Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by wav · Nov 08, 2012 at 07:00 PM · animationaudiotime.deltatimefootsteps

footstep audio sync probs

Hi. SECOND time writing this out, thanks a lot UDN!!

anyway, grumbling aside I'm having trouble synching footsteps sounds to prerendered spritesheets.

previously I had written a script that played audio based on 3 things:

1.the frames in the animation

2.the frame where the first footfall is

3.and the second

this worked for a while, but I'm noticing the audio getting out of sync over time.

I'm sure there is some way to do it using Time.deltaTime, but I still cant fully get my head around that.

Also to add more complexity (of course, nothing is easy! :P ) different animations have different FPS's.

Here's the script I currently have:

 using UnityEngine;
 using System.Collections;
 
 public class WalkingAudio : MonoBehaviour {
     
     public AudioClip[] GrassSteps; // contains grass step sounds    
     public AudioClip[] StoneSteps; // contains stone step sounds
     public AudioClip[] GravelSteps; // contains gravel step sounds
     public AudioClip[] MetalSteps; // contains metal step sounds
     public AudioClip[] WoodSteps; // contains metal step sounds
     
     public bool isMoving = false; // is the character moving?
     
     public int firstFootfall; // the frame number where the first foot hits the ground
     
     public int secondFootfall; // the frame number where the second foot hits the ground
     
     public int framesInAnimation; // the amount of frames in the animation
     
     public enum GroundType {Grass, Stone, Gravel, Metal, Wood}; // what kind of surface is the character on
     
     private float currentFrame; // the current frame
     
     AudioClip[][] AudioArrayHolder = new AudioClip[5][]; // create an array to hold the three different audio arrays
     
     private int surface;    // 
     
     public GroundType ground;
     
     public GroundType previous;
     
     public float audioMax;
     
     public float audioMin;
     
     public float volDipTime;
     
     float audioMaxStart; // saves the original 
     
     float audioMinStart;
     
     bool dipVolume = false; // should the vol be dipped?
     
     // Use th for initialization
     void Start () {
         
         /*if (ground == null){
         ground = GroundType.Stone;
         }*/
         StartCoroutine("NewMaterial");    
     }
     
     void Awake(){
         currentFrame = 1;
         AudioArrayHolder[0] = GrassSteps;
         AudioArrayHolder[1] = StoneSteps;
         AudioArrayHolder[2] = GravelSteps;
         AudioArrayHolder[3] = MetalSteps;
         
         audioMaxStart = audioMax; // save original values to reset
         audioMinStart = audioMin;
         
         //Debug.Log("audioMaxStart in Start"+audioMaxStart);
     }
     // Update is called once per frame
     void Update () {
         
         //    Debug.Log("isMoving "+isMoving);
         //what kind of ground is the character on?
         switch (ground){
             
         case GroundType.Grass:
             surface = 0;
             
         break;
             
         case GroundType.Stone:
             surface = 1;
             //StartCoroutine("NewMaterial");
         break;
             
         case GroundType.Gravel:
             surface = 2;    
             //StartCoroutine("NewMaterial");
         break;
             
         case GroundType.Metal:
             surface = 3;
             //StartCoroutine("NewMaterial");
         break;
             
         case GroundType.Wood:
             surface = 3;    
             //StartCoroutine("NewMaterial");
         break;
         }
         
         
         // if the character is moving play the sounds
         if (isMoving){
             //Debug.Log("surfacee no is:"+surface);
             currentFrame ++;//
             
             //Debug.Log("currentFrame "+currentFrame);
             
             //play the first footfall on the relevant frame
             if (currentFrame == firstFootfall){
                 audio.volume = Random.Range(audioMin,audioMax );
                 audio.PlayOneShot(AudioArrayHolder[surface][Random.Range(0, (AudioArrayHolder[surface].Length-1))]);    
                 
                 //Debug.Log("playing footf1");
             }
             
             //play the second footfall on the relevant frame
             if (currentFrame == secondFootfall){
                 audio.volume = Random.Range(audioMin,audioMax );
                 audio.PlayOneShot(AudioArrayHolder[surface][Random.Range(0, (AudioArrayHolder[surface].Length-1))]);    
                 
                 //Debug.Log("playing footf2");
             }
             // if we have reached the end of the animation end go back to the start
             if (currentFrame == framesInAnimation ) {
                 currentFrame = 1;    
             }
         }
     //    Debug.Log("dipVolume"+dipVolume);
         
         if (dipVolume ){
             //Debug.Log("audiomax reducing");
             //Debug.Log("audiomaxstart"+audioMaxStart);
             if (audioMax > audioMaxStart/2 || audioMax == audioMaxStart ){
                 audioMax -= 0.01f; 
             }
         }
     }
     
     public void StartMoving() {
         isMoving = true;
         //Debug.Log("started moving "+isMoving);
     }
     
     public void StopMoving() {
         isMoving = false;
         //Debug.Log("stopped moving "+isMoving);
     }
     
     
     
 }
 
Comment
Add comment · Show 8
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 Seth-Bergman · Nov 08, 2012 at 07:12 PM 0
Share

but where is the animation code this is meant to sync to?

avatar image wav · Nov 08, 2012 at 07:21 PM 0
Share

we're using sprite $$anonymous$$anager 2, so there is no animation code really - hence me wanting to sync the sound using frames...

its all 2d spritesheets

avatar image wav · Nov 08, 2012 at 07:22 PM 0
Share

basically when the character starts moving Start$$anonymous$$oving() is called, and when he stops, Stop$$anonymous$$oving()

avatar image Seth-Bergman · Nov 08, 2012 at 07:44 PM 0
Share

in other words, there is no actual direct correlation between "currentFrame" and the current frame of the animation.. so how exactly do you expect this to sync up? You would need to access the ACTUAL current frame, from the sprite $$anonymous$$anager..

avatar image wav · Nov 08, 2012 at 07:50 PM 0
Share

well they are both started at the exact same time.

But yes I realise that this approach isnt working.

Theres an inbuilt thing to do this in Spritemanager 2, but it is very costly performance-wise.

As they are started at the same time, I'm sure there is some way to use deltatime to work out the exact length in time between keyframes?

Show more comments

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.

Follow this Question

Answers Answers and Comments

12 People are following this question.

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

Related Questions

Footstep sounds when walking 7 Answers

GameObject reacts to audio source 1 Answer

Synchronize Audio and Animation 0 Answers

Can I make animations snap to a frame? 1 Answer

My Footstep isn't working 2 Answers


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