- Home /
Rhythm Game Out of Sync (C#)
Hello, I currently have objects spawning to the beat of the music. However, when they hit the player's prompt, they are out of sync because of the time taken to travel from the left to the right of the screen.
The music is coming from the main camera, I was thinking of having 2 separate music tracks. 1 for the player to hear, which is delayed by a couple seconds, and the one that the code listens to to spawn the objects. Would this work? Any ideas how to implement this?
Thanks.
Here is how the beats are listened to/objects are spawned.
public Transform target;
public int minRange;
public bool follow;
//public Light spotlight;
private float speed;
public Rigidbody projectile;
private float cooldown = 0.5f;
public int detail = 250;
public float amplitude = 0.2f;
public float minValue = 1.0f;
void Start () {
}
void Update(){
cooldown -= Time.deltaTime;
float[] info = new float[detail];
AudioListener.GetOutputData(info, 0);
float packagedData = 0.0f;
for(int x = 0; x < info.Length; x++)
{
packagedData += System.Math.Abs(info[x]);
}
if (packagedData/info.Length > 0.2f && cooldown < 0) {
cooldown = Random.Range(1f,1.5f);
Rigidbody clone;
clone = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody; //This seems to create the clone
clone.velocity = transform.TransformDirection(Vector3.forward * 40); //This details the velocity of the projectile
}
}
}
Answer by HDX13 · Aug 25, 2017 at 02:48 PM
My take on this would be delaying the audio source playback. There are either two methods to achieve this:
AudioSource music = FindObjectOfType<AudioSource>();
music.PlayDelayed(delayTime);
// or
music.PlayScheduled(delayTime);
Both uses seconds, but the differences are:
PlayDelayed is synced with the time of the game and uses float
PlayScheduled is synced with the AudioSettings.dspTime and uses double
Plus, make sure to set "Play On Awake" to false. And let the beat generation runs as usual.
Your answer
Follow this Question
Related Questions
Increasing spawn rate over time 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
LineRenderer static movement 1 Answer
Is there something more reliable than simple Triggers for something like a Rhythm Game? 1 Answer