Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Figuring Out How Fast an Object Should Move (Music Game + Scrolling Track)

Discussion in 'Scripting' started by fryedrycestyle, Sep 2, 2014.

  1. fryedrycestyle

    fryedrycestyle

    Joined:
    Jun 8, 2012
    Posts:
    19
    Hey guys,

    I'm making a music game similar to DJ MAX Portable or Beatmania. I'm currently rewriting a portion of the code so the notes on the track all move as one object, instead of being spawned N seconds before they need to reach the "NOW" bar.

    However, I'm having difficulty with the track moving down fast enough with the music.

    It comes down to some math - how to calculate the speed. And that's where I'm having issues.

    Most know, speed = distance / time, right? Well I'm having difficulty with using the right values for distance/time.

    I'm scrolling the track based on the position of the first note - which corresponds to the timestamp in the song when it is played. In this case it is 10625 on the Y axis. I need it to reach Y = 32 at that time. The first note is a child object of a "Track" object, whose position is 0. The track is then moved at the speed calculated based on the first note.

    So I have:

    Code (CSharp):
    1. float distance = firstNote.position.y - 32; // 32 needs to be changed to a variable
    As for the actual calculation of speed I have this:
    Code (CSharp):
    1. float tStamp = (float) firstNote.GetComponent<MIDINoteGem> ().TimeStamp;
    2.  
    3. return distance / (tStamp - _noteGen.AppearWindow);
    4.  
    Where _noteGen.AppearWindow is equal to 2500 ms, but can be changed based on difficulty, etc. AppearWindow is essentially how long the note appears on the screen prior to hitting the "NOW" bar - affecting how fast it travels.

    So what am I doing wrong? I'm guessing its something silly / obvious, but I'm hitting a mental block between working on this, school work, and work work!

    Thanks in advance!
     
  2. fryedrycestyle

    fryedrycestyle

    Joined:
    Jun 8, 2012
    Posts:
    19
    Just to give a bit more information...

    Code (CSharp):
    1.     public IEnumerator moveNotes (Transform track, Queue<MIDINoteGem> notes, float speed) {
    2.  
    3.         while (_thisAudio.isPlaying) {
    4.             if (notes.Count > 0) {
    5.  
    6.                 Vector3 updatedPos = track.position;
    7.                 updatedPos.y -= speed * (Time.deltaTime * 1000.0f);
    8.  
    9.                 track.position = updatedPos;
    10.  
    11.                 yield return new WaitForFixedUpdate();
    12.             }
    13.  
    14.             yield return 0;
    15.         }
    16.  
    17.     }
    This is the code where the track actually moves. Could there be something being done wrong here? Speed is equal to the return statement in the previous post.
     
  3. fryedrycestyle

    fryedrycestyle

    Joined:
    Jun 8, 2012
    Posts:
    19
    *bump*

    I really need some help figuring this out, does nobody have any tips?