Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

How to detect a Key is not being hold down when you're suppose to [Resolved]

Discussion in 'Scripting' started by Pixitales, Mar 16, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Its a guitar/rhythm game and I am working on a hold note. A hold note is a long vertical line. When it collides with the button(tag Activator), you have to hold it until it reaches the top end.

    Debug.Log "Holding" and "Not Holding Down" works but its not suppose to say "Not Holding down" when you're holding a key down. Not sure if "!Input.GetKeyDown(keyToPress)" is right or something is missing...

    Code (CSharp):
    1. public class NoteHoldObject : MonoBehaviour
    2. {
    3.     public bool canBeHolded;
    4.     public KeyCode keyToHold;
    5.  
    6.     void Update()
    7.     {
    8.         if (Input.GetKeyUp(keyToPress))
    9.         {
    10.             if (canBeHolded)
    11.             {
    12.                 Debug.Log("Holding");
    13.                 Debug.Log(transform.position);
    14.             }
    15.         }
    16.     }
    17.  
    18.     private void OnTriggerStay(Collider other)
    19.     {
    20.         if (other.tag == "Activator")
    21.         {
    22.             canBeHolded = true;
    23.             {
    24.                 if (Mathf.Abs(transform.position.z) <= 10f && !Input.GetKeyDown(keyToPress))
    25.                 {
    26.                     Debug.Log("Not Holding Down");
    27.                 }
    28.             }
    29.         }
    30.     }
     
    Last edited: Mar 16, 2019
  2. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Nevermind, I fixed it... Its !GetKey, not !GetKeyDown.