Search Unity

Question How can i check wich note to hit first in a rhythm game?

Discussion in 'Scripting' started by pronixer132sd, Oct 24, 2021.

  1. pronixer132sd

    pronixer132sd

    Joined:
    Aug 16, 2020
    Posts:
    2
    So im doing a rhythm game and i have everything done (loading charts, bpm, etc)
    but i encountered this problem while play-testing

    whenever the notes are too close to each other both get pressed instead of the first one
    I tried making a script wich constantly sets the first index of the corresponding note array to "can be pressed" state, but that makes some notes just stop working

    code im using to set the note state to "can be pressed":

    Code (CSharp):
    1. PlayState.notesLeft[0].mustBePressed = true
    2. //and this repeats for the 4 note arrays.
    code im using to spawn the notes:

    Code (CSharp):
    1. public void SpawnNote(NoteData note, float time)
    2.     {
    3.         int transformID = 0;
    4.  
    5.         switch (note.nType)
    6.         {
    7.             case NoteType.Down:
    8.                 transformID = 1;
    9.                 break;
    10.             case NoteType.Left:
    11.                 transformID = 0;
    12.                 break;
    13.             case NoteType.Up:
    14.                 transformID = 2;
    15.                 break;
    16.             case NoteType.Right:
    17.                 transformID = 3;
    18.                 break;
    19.         }
    20.  
    21.         var nObj = Instantiate(noteGameObject, new Vector3(NotePos[transformID].position.x, time, -9.23f), Quaternion.identity).GetComponent<Note>();
    22.  
    23.         nObj.transform.parent = scroller.transform;
    24.      
    25.         nObj.Set(note.nType);
    26.  
    27.         switch (note.nType)
    28.             {
    29.                 case NoteType.Left:
    30.                     notesLeft.Add(nObj);
    31.                     break;
    32.                 case NoteType.Right:
    33.                     notesRight.Add(nObj);
    34.                     break;
    35.                 case NoteType.Up:
    36.                     notesUp.Add(nObj);
    37.                     break;
    38.                 case NoteType.Down:
    39.                     notesDown.Add(nObj);
    40.                     break;
    41.             }
    42.     }
    if anyone knows how to fix it please let me know
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    One thing you could do is Instantiate each note further back on the z axis than the last. Then, when checking which note to hit when pressing a button, you would shoot a RayCast with direction (0, 0, 1), and it would stop at and return the first note hit.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Might be perhaps an order of script execution issue. I'm not sure when you are giving up on the player hitting that current note (note zero) and perhaps you need to allow the player to be slightly late on that note?? I'm not really sure of your methodology as far as grading the note hits.

    Either way, you must find a way to get the information you need in order to reason about what the problem is.

    It might be easiest to make a short 3-note song and use it over and over again to debug, checking which code executes when with the following method:

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494