Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question input key is always pressed

Discussion in 'Input System' started by Kire_, Aug 15, 2020.

  1. Kire_

    Kire_

    Joined:
    Jul 21, 2020
    Posts:
    13
    I tried with several keys and they all work as if they were always pressed :(

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Porta1A2 : MonoBehaviour
    7. {
    8.     public bool open;
    9.     public GameObject rotate;
    10.  
    11.     void Start()
    12.     {
    13.         open = false;
    14.     }
    15.  
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetKey(KeyCode.F));
    20.         {
    21.             Debug.Log("presss");
    22.             open = true;
    23.  
    24.         }
    25.         if (open == true)
    26.         {
    27.  
    28.             transform.localRotation = Quaternion.Lerp(transform.localRotation, rotate.transform.localRotation, 2);
    29.         }
    30.     }
    31.  
    32. }
     
  2. coderD1mka

    coderD1mka

    Joined:
    Jun 26, 2019
    Posts:
    8
    Lol =) Why You don`t set your "open" variable to the false when the key F is unpressed? In Update You only set it to the true.
     
  3. Kire_

    Kire_

    Joined:
    Jul 21, 2020
    Posts:
    13
    sttil not working... it's like the key is always pressed, even if I press it, it doesn't stop
     
  4. coderD1mka

    coderD1mka

    Joined:
    Jun 26, 2019
    Posts:
    8
    Show me your new code.
    You need something like this:

    if (Input.GetKey(KeyCode.F))
    {
    Debug.Log("presss");
    open = true;
    }
    else
    open = false;

    Or more better replace whole code above on this :

    open = Input.GetKey(KeyCode.F);
    Debug.Log($"press={open}");


    And in your code one place is very suspiciously:

    if (Input.GetKey(KeyCode.F)); <--- delete this semicolon
     
    Last edited: Aug 15, 2020
  5. Kire_

    Kire_

    Joined:
    Jul 21, 2020
    Posts:
    13
    omg that worked tanks, but now i can´t use esc key XD the key it´s not broke in my keyboard but it´s not woking in unity...


    if (Input.GetKeyDown(KeyCode.Escape))
    {
    MenuEsc = true;
    }
    if(MenuEsc == false)
    {
    onibus.GetComponent<VehicleController2017>().enabled = true;
    escMenu.SetActive(false);
    }
    if (Input.GetKeyDown(KeyCode.Escape))
    {
    MenuEsc = false;
    }
    if(MenuEsc == true)
    {
    onibus.GetComponent<VehicleController2017>().enabled = false;
    escMenu.SetActive(true);
     
  6. coderD1mka

    coderD1mka

    Joined:
    Jun 26, 2019
    Posts:
    8
    That`s all you need:

    MenuEsc = Input.GetKeyDown(KeyCode.Escape);
    onibus.GetComponent<VehicleController2017>().enabled = !MenuEsc;
    escMenu.SetActive(MenuEsc);