Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Input.GetKeyUp Help (C#)

Discussion in 'Scripting' started by Food_Lover, Feb 16, 2015.

  1. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    I would just like to confirm one thing, will "It will not return true until the user has pressed the key and released it"? (quoted from http://docs.unity3d.com/ScriptReference/Input.GetKeyUp.html) or does it return true even just when the player lets the button go. I would just like to know because if that's the case then my code should work.
     
  2. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Just out of curiosity, when is he going to press it? During scene load?
     
  3. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    In the update method
     
  4. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Hmm... so for the user/player to release the key he first has to press it?
     
  5. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    It will return true at the time you release
    the key only..i.e it remains false all the time
    but becomes true just when you release the key
    and then again immediately in the next frame it becomes false again.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,183
    It's not possible to let go of a button unless you have pressed it first. That's #3 in the Fundamental Laws of How Keyboards Work.

    If you're trying to do this:

    Code (csharp):
    1. if(Input.GetKeyDown(KeyCode.someKey)) {
    2.     if(Input.GetKeyUp(KeyCode.someKey)) {
    3.         foo.DoTheBar();
    4.     }
    5. }
    It won't work - a key isn't both down and up on the same frame. It might be physically possible if you have really fast fingers, but I'm pretty sure that either the OS or Unity spreads those two events out over time.
     
    jtsmith1287 likes this.
  7. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    Question how do you make that little box for the code?

    Ok well I had that and I figured it wouldn't work so I went with just:
    1. if(Input.GetKeyUp(KeyCode.someKey)) {
    2. //code
    3. }
    but that didnt seems to work either, Im not at home right now and can't check the code but I will reply my code when i do.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,183
    You get the box from clicking "insert -> code".

    Input.GetKeyUp works, so it's definitely a bug in your code somewhere. It's easier to help if you actually post it.
     
    Food_Lover likes this.
  9. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    Ok Thanks :) I get put the code up later today
     
  10. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    This is the part of the code that isn't working, I want it to update the variable but It stay's the same

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class VariableChangeTest : MonoBehaviour {
    6.     public int Side = 1;
    7.     void FixedUpdate () {
    8.         if( Input.GetKeyUp(KeyCode.Comma) == true && Side == 1 ) {
    9.             Side = 2;
    10.         }
    11.     }
    12. }
    13.  
    EDIT: Does it matter whether I use fixed update or a normal update ( I heard fixed update is better )
     
  11. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Clearly you are not a tester :p
    What if I take the focus off the game, press the key, return focus to the game, and then release the key?
    Players tend to try many things to break the game y'know.
     
    kdubnz and Food_Lover like this.
  12. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    Wait, so is there something wrong with the code?
     
  13. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,183
    GetKeyUp/Down changes every Update frame. This means that they are not reliable in FixedUpdate. Try to do the same thing in Update, and it'll be fine.

    if you need to react to the input in FixedUpdate, poll the input in Update, and store some bool that you read in fixed:

    Code (CSharp):
    1. public int side = 1;
    2. public int side = 1;
    3.  
    4. void Update() {
    5.     if(Input.GetKeyUp(KeyCode.Comma)
    6.         commaUnpressed = true;
    7. }
    8.  
    9. void FixedUpdate() {
    10.     if(commaUnpressed) {
    11.         commaUnpressed = false;
    12.         side = 2;
    13.     }
    14. }
     
    Food_Lover likes this.
  14. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    FixedUpdate is used for physics-related calculations, since you'd want it to work in fixed intervals.
    That would mean that if someone leaves and presses a key again, the actions may fall on times that the FixedUpdate was not there to catch these commands.
     
    Food_Lover likes this.
  15. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    Ok Thanks, I'll test it
     
  16. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    Ok it still dosn't work, but here's by full code (Pretty much i just want this camera to rotate around a 7x7 cube)
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraControl1 : MonoBehaviour {
    6.     public int Side = 1;
    7.     void Update () {
    8.         if( Input.GetKeyUp(KeyCode.Z) == true && Side == 1 ) {
    9.             transform.Translate(-4,0,+4);
    10.             Side = 2;
    11.         }
    12.         if( Input.GetKeyUp(KeyCode.Z) == true) {
    13.             transform.Translate(+4,0,+4);
    14.             Side = 3;
    15.         }
    16.         if( Input.GetKeyUp(KeyCode.Z) == true) {
    17.             transform.Translate(+4,0,-4);
    18.             Side = 4;
    19.         }
    20.         if( Input.GetKeyUp(KeyCode.Z) == true) {
    21.             transform.Translate(-4,0,-4);
    22.             Side = 1;
    23.         }
    24.     }
    25. }
    26.  
     
  17. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    At first glance I can say one thing : All of your if clauses are going to run simultaneously when the Z
    key is lifted.

    Maybe, you wanted to add a && side == x etc in each of the if clause ?
     
    ThermalFusion likes this.
  18. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    ugh.. all those useless if statements...

    Code (csharp):
    1. void Update() {
    2.   if (Input.GetKeyUp(KeyCode.Z)) {
    3.     //do things
    4.     switch(Side) {
    5.       case 1: //do things
    6.       case 2: //do things
    7.     }
    8.   }
     
  19. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    Yeh, your right, I should add that, I just did so much fiddling I forgot to add it in
     
  20. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Still ugly ! You should use break with switch statements (otherwise your code will continue to check all cases).

    Code (CSharp):
    1. public class CameraControl1 : MonoBehaviour {
    2.     public int Side = 0;
    3.  
    4.     private Vector3[] sideVectors = new Vector3[4];
    5.  
    6.     void Awake() {
    7.         sideVectors[0] = new Vector3(-4, 0, 4);
    8.         sideVectors[1] = new Vector3(4, 0, 4);
    9.         sideVectors[2] = new Vector3(4, 0, -4);
    10.         sideVectors[3] = new Vector3(-4, 0, -4);
    11.     }
    12.  
    13.     void Update () {
    14.         if(Input.GetKeyUp(KeyCode.Z) == true) {
    15.             transform.Translate(sideVectors[Side]);
    16.             Side++;
    17.  
    18.             if (Side >= sideVectors.Length) {
    19.                 Side = 0;
    20.             }
    21.         }
    22.     }
    23. }
    /codeBattle
     
    dterbeest likes this.
  21. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    You can't even use a switch without break statement..(will through compiler error) so it was kind off implied
     
  22. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    It's quite possible when the frame rate is low due to whatever reasons. It's easy to simulate:
    Code (csharp):
    1.     void Start()
    2.     {
    3.         Application.targetFrameRate = 1;
    4.         QualitySettings.vSyncCount = 0;
    5.     }
    6.  
    7.     void Update()
    8.     {
    9.         if (Input.GetKeyDown(KeyCode.Alpha7))
    10.         {
    11.             Debug.Log("7 is down");
    12.             if (Input.GetKeyUp(KeyCode.Alpha7))
    13.             {
    14.                 Debug.Log("7 is up");
    15.             }
    16.         }
    17.     }
    18.  
    :)
     
  23. Food_Lover

    Food_Lover

    Joined:
    Dec 29, 2014
    Posts:
    16
    Um, Im still fairly new to all this so that's a tad confusing