How to script a long note in Unity rhythm game? – For beginners

0

Salvation. 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 you have to hold for a while . I’ve followed a few tutorials but none of them show how to do it properly.

Here is my current code for tap notes (you only need to press the key 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, which can be obtained by multiplying the length of the note sprite collider by the song tempo and create a conditional loop to check if I held enough time . But I don’t know how to make the note hold visually without destroying it when it just enters the button collider (like in the screenshot below). I’m a newbie to both programming and unity, so I don’t know much. Does anyone know how to do it correctly?

Any help would be appreciated.

(sorry if there will be grammar mistakes, I hope what I wrote is understandable)

Share.

Comments are closed.