Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. 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.

Question How to do this once in Update?

Discussion in 'Scripting' started by alainjiehfeng, Jun 16, 2020.

  1. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Code (CSharp):
    1. private int directions = 1;
    2. var keyboard = Keyboard.current;
    3.  
    4. if (keyboard.shiftKey.isPressed)
    5.         {
    6.             directions++;
    7.  
    8.             if (directions == 5)
    9.             {
    10.                 directions = 1;
    11.             }
    12.  
    13.         }
    I want to add 1 to directions every time the shift key is pressed. Since I need to check the key press, I have to do it in update, but in update this code is executed multiple times and the directions goes all the way to 4 before going back to 1.

    How do I make it so that every time the shift key is pressed, directions is added by 1 and only 1? Thanks in advance. :)
     
  2. Try .wasPressedThisframe instead of isPressed
     
    alainjiehfeng likes this.
  3. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Brilliant! It works thanks so much! :D
     
    Lurking-Ninja likes this.