Rhythm game and interpolation question
I've been following these two tutorials (this one and this one) as well as looking at the author of the second one's open source game (as they didn't write their tutorial very well) and I've ended up with these three scripts:
number one, the conductor, which keeps track of the bpm and things, as well as just generally running everything:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Conductor : MonoBehaviour
{
public float songBpm;
public float secPerBeat;
public float songPos;
public float songPosBeats;
public float dspSongTime;
public AudioSource musicSource;
public float firstBeatOffset;
public float beatsPerLoop;
public int completedLoops = 0;
public float loopPosBeats;
public float LoopPosAnalog;
public static Conductor instance;
public float eigthNotes;
public float songPosEigths;
public float sixteenNote;
public float songPosSixteen;
public float[] notes;
public int nextIndex;
public float advanceBeats = 3;
public GameObject NoteGO;
private void Awake()
{
instance = this;
}
void Start()
{
musicSource = GetComponent<AudioSource>();
secPerBeat = 60f / songBpm;
dspSongTime = (float)AudioSettings.dspTime;
musicSource.Play();
notes = new float[] { 1, 2, 3, 4, 5 };
eigthNotes = secPerBeat / 2;
sixteenNote = secPerBeat / 4;
}
// Update is called once per frame
void Update()
{
songPos = (float)(AudioSettings.dspTime - dspSongTime - firstBeatOffset);
songPosBeats = songPos / secPerBeat;
songPosEigths = songPos / eigthNotes;
songPosSixteen = songPos / sixteenNote;
//Debug.Log(notes.Length);
if (nextIndex < notes.Length && notes[nextIndex] < songPosBeats + advanceBeats)
{
BeatMove musicNote = NotePool.instance.GetNote(-10, 10, 4.6f, 0, notes[nextIndex]);
nextIndex++;
}
if(songPosBeats >= (completedLoops +1) * beatsPerLoop)
{
completedLoops++;
}
loopPosBeats = songPosBeats - completedLoops * beatsPerLoop;
LoopPosAnalog = loopPosBeats / beatsPerLoop;
}
}
number two, the Note Pool script, in charge of spawning all of the notes and turning them on as well as giving each of them an individual notes[nextIndex] value:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NotePool : MonoBehaviour
{
public static NotePool instance;
public GameObject notePrefab;
public int initialAmount;
private List<BeatMove> objList;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
objList = new List<BeatMove>();
for (int i = 0; i < initialAmount; i++)
{
GameObject obj = (GameObject)Instantiate(notePrefab);
obj.SetActive(false);
objList.Add(obj.GetComponent<BeatMove>());
}
}
public BeatMove GetNote( float startX, float endX, float posY, float posZ, float beat)
{
foreach (BeatMove note in objList)
{
if (!note.gameObject.activeInHierarchy)
{
note.Initialize(startX, endX, posY, posZ, beat);
note.gameObject.SetActive(true);
return note;
}
}
BeatMove musicNote = ((GameObject)Instantiate(notePrefab)).GetComponent<BeatMove>();
musicNote.Initialize(startX, endX, posY, posZ, beat);
objList.Add(musicNote);
return musicNote;
}
}
and number three, the note code itself, which is in charge of interpolation and such:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeatMove : MonoBehaviour
{
float t;
public Vector3 startPosition;
public Vector3 target;
float timeToReachTarget;
public static float thisNoteBeat;
float timeElapsed;
public static BeatMove instance;
private void Start()
{
this.gameObject.active = true;
instance = this;
startPosition = transform.position;
target = new Vector3(0, transform.position.y);
}
public void Initialize(float startX, float endX, float posY, float posZ, float beat) {
void Update()
{
Debug.Log(thisNoteBeat);
transform.position = new Vector3(startPosition.x + ((target.x - startPosition.x) * 1f) - beat - thisNoteBeat - Conductor.instance.songPosBeats / Conductor.instance.advanceBeats, transform.position.y, transform.position.z);
}
}
}
and what this code is supposed to do is create a note on beats 1, 2, 3, 4, and 5, and smoothly move them to (0, 0) to the beat, so that it remains a rhythm game, but so far all it does is spawn immobile beats in the corner of the screen...
any help you can give would be appreciated, and if you see any spaghetti code, don't hesitate to help!