I’ve read some guides about this, but still can’t wrap my head around this.
So basically I am making a rhythm game that has a beat bar with individual beats that spawn on a beat and move from the right side of the screen to the left until they hit a beat trigger, after which, they either keep going or get deactivating, depend on whether or not the player hit a button in time. I’ve got them spawning correctly and on beat.
My problem is that I’m trying to do this through interpolation and have no idea how to do it this way. I have this block of code in the script that is supposed to move the notes, but I don’t understand why it’s not working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NoteMover : MonoBehaviour
{
public Image Spawner;
public Image BeatTrigger;
public Conductor cond;
void Update()
{
transform.position = Vector3.Lerp(
Spawner.transform.position,
BeatTrigger.transform.position,
(cond.beatsShownInAdvance - (cond.notes[cond.nextIndex-1] - cond.songPositionInBeats)) / cond.beatsShownInAdvance);
}
}
“cond.beatsShownInAdvance” shows how many beats are shown on the screen before the song starts
“cond.notes[cond.nextIndex-1]” is the beat of this note
“cond.songPositionInBeats” is the current beat of the song
So, the whole third argument is basically how fast the notes should be moving, if I understand correctly, but in my current case, they don’t move. I currently have no beats in advance, which is probably the thing that is breaking it, tbh.
I think I may need two scenarios here, one is where I don’t have any beats shown in advance and one where I do.
I really appreciate the advice, thanks in advance!
No. In a Lerp, the third argument is what fraction of the way from the start to the end you should be at this exact moment. If you want to do something based on speed rather than % completion, you should be somehow adding to the current position (perhaps using transform.Translate), not overwriting it, and shouldn’t be using Lerp.
But I don’t see how your third argument makes sense as either of those things.
I’d probably approach this problem with something along these lines:
Ah, yeah. that makes a lot more sense. I was a bit frustrated and didn’t really want to think about what I wanted the notes to do, and, as seejayjames correctly interpreted, I was coding at 5 am XD
Anyway, I’ve thought about it a bit more and what I want the notes to do is make it from the spawner position to the beat trigger position in a total of 8 beats (so, if the note spawner on beat 1, it’s at the trigger at beat 9). I’ve removed the “notes shown in advance” feature entirely, since it doesn’t make sense to do it that way now that I’ve implemented an offset time before the song starts playing. And I looked it up and lerp is a pretty good way to move the beats if you want to make them move in a percentage-based way.
So, basically, my movement function now looks the same as it was,
transform.position = Vector2.Lerp(Spawner.transform.position,BeatTrigger.transform.position,/* I have no idea what to put here*/);
But I still don’t know what to put in the third argument so that it outputs a percentage of distance between the two objects I’m moving the beat between, so that it’s based on the dspTime system and not pixels or deltaTime, etc.
But it moves the note from start to finish in one beat.
“beatSpawnSongBeat” is a variable I added to keep track of when the note spawned
Edit: I thought it was too simple to work, but just divide it by the number of beats that have to go by until the note gets to the trigger. In my case, that’s 8. So I divided the whole third argument by my beatToStartOn variable from my conductor class minus one.