Advertisement

How to script a long note in unity rhythm game?

Started by July 19, 2021 10:09 PM
3 comments, last by Shaarigan 3 years, 2 months ago

Hello. I'm developing a vertical scrolling rhythm game(like step mania, friday night funkin) and I ran into the problem that I can't figure out how to make a long note, a note that you need to hold for a certain amount of time. I've been following some tutorials but none of them shows how to do it properly.

Here's my current code for the tap notes(you need to hit the key only once):

void Update()
    {
        if (Input.GetKeyDown(keyToPress))
        {
            if (canBePressed)
            {
                gameObject.SetActive(false);
                Debug.Log("Hit");
            }
        }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Activator")
        {
            canBePressed = true;
        }
    }
    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.tag == "Activator")
        {
            canBePressed = false;
            Destroy(gameObject, 0.25f);
        }
    }
}

I thought maybe I should create a variable TimeToBeHeld, that can be obtained by multiplying the length of the note sprite collider by the tempo of the song and create a conditional loop to check if I held enough time. But I don't know how to make note being held visually without destroying it when it just got into the collider of the button(like on the screenshot below). I'm a beginner at both programming and unity so I don't know much. Does anyone know how to do it right?

Any help will be appreciated.

(sorry if there will be any grammar mistakes, I hope it's understandable what I've written)

StepMania - Wikipedia

I'm sorry but I can't see any reason to frequently create and destroy game objects, nor to involve physics here. If you play a track, then the game should create the same result for multiple runs and this can't be guaranteed, for example when heap must be allocated first to create the game object.

Instead of doing what you're doing at the moment (this might be simple but simple isn't always good), you should instead listen to the time the track is already running and define ranges for every key to be pressed or held along that time. I'tll not just increase performance but also eliminate your trigger problem.

Consider using a pool of objects to be displayed, UI should never ever have a physics component

Advertisement

@Shaarigan Thanks for the reply, but how can I manage the order of creating of the objects with pool? The notes should follow the melody of a track but in the articles and videos about object pooling that I've seen they often use the basic generation of the objects.(which can be set with a single formula) In my project I just drag and dropped the objects in the scene, make them scroll down and then destroy. (it's basic and stupid but I don't know better) Can I set a certain position or order when the objects should be pooled or something like that? Or is it not meant for this and I should use something else?

sangiria said:
but I don't know better

Maybe this is your issue. First learn the programming language, then make your game. Following a tutorital without knowing what and why they're doing is worthless on a learning purpose.

In general, you can always set an objects position if you take it out of a pool or create it on demand. It doesn't matter as objects are simply data. The problem of creating and destroying too many objects in Unity comes from the CLR (but it happens as well in other languages) which contains a garbadge collector. This is simply spoken a system which defrags your heap memory by moving object data around whenever it hits a border. If you frequently create new objects, so allocate memory from the heap, garbadge gets more and more until the GC is getting into the game and blocks the thread for doing some cleanup. This will sooner or later cause a hickup in your game which can lead to a delay between your music and the rythm pattern.

Instead you should couple your music track with a schedule which tells the game what rythm pattern should be at which position, based on the time already gone in the track. For example show the X arrow from second Y to second Z of the track

This topic is closed to new replies.

Advertisement