Search Unity

Question Using MIDI piano as a keyboard (or just as an input method)

Discussion in 'Input System' started by karsten-heim, Jan 23, 2023.

  1. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    63
    I am aware that plugins exist for making a MIDI piano compatible with unity for a game, but would it be possible to use each individual key as a designated button? (like keys on a keyboard). I know that this is at least possible outside of unity (like using external plugins or the fact that GarageBand is able to record MIDI instruments in real time), but I would like to know if it is even within the bounds of possibility to use a MIDI piano as an input method without a plugin.

    Here is more or less what I was thinking that I want if it turns out to be possible to use a MIDI piano as an input method:

    Code (CSharp):
    1. private bool sustain_on;
    2. public AudioSource piano;
    3. public AudioClip pianokey_note_A;
    4. void Start(){
    5.     StartCoroutine(playpiano);
    6.     piano = GetComponent<AudioSource>();
    7. }
    8. void Update()
    9. {
    10. if(Input.GetKeyDown(pianokey1) && sustain_on == false)
    11. {
    12.      piano.PlayOneShot(pianokey_note_A, 0.75f);
    13. }
    14. if(Input.GetKeyDown(pianokey1) && sustain_on == true)
    15. {
    16.      piano.PlayOneShot(pianokey_note_A, 0.75f);
    17.      //but extend the length of the pianokey sound
    18. }
    19.  
    20. // but with this method i would have to have over 160 if statements, so this would work but not be convenient


    this is also pretty important that it be in a script, because the piano is going to be the sole controller of my game, so I need to have it be customizable.

    -Thanks:D
     
    Last edited: Jan 24, 2023