- Home /
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);
}
}
but where is the animation code this is meant to sync to?
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
basically when the character starts moving Start$$anonymous$$oving() is called, and when he stops, Stop$$anonymous$$oving()
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..
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?
Your answer
Follow this Question
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