Search Unity

Help whit keys script

Discussion in 'Scripting' started by Nixel2013, Aug 3, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    I would like to know how I can do to put two keys where it says "if (Input.GetKeyDown (KeyCode.Escape))" I would like that function to be activated with the E key and Escape


    Code (CSharp):
    1. public class CurcorHideShow : MonoBehaviour
    2. {
    3.     public bool isLocked = true;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.Escape))
    14.         {
    15.             isLocked = !isLocked;
    16.         }
    17.         Cursor.lockState = isLocked ? CursorLockMode.Locked : CursorLockMode.None;
    18.         Cursor.visible = !isLocked;
    19.     }
    20. }
     
  2. RustyCrow

    RustyCrow

    Joined:
    Feb 28, 2014
    Posts:
    43
    If i understand you correctly its a simple && or || in the if() -> check this out
    && Escape and E needs to be pressed
    || Escape or E needs to be pressed

    eksample :

    Code (CSharp):
    1. public class CurcorHideShow : MonoBehaviour
    2. {
    3.     public bool isLocked = true;
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.     }
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.E)) // Or if both needs to be pressed use && insted of ||
    12.         {
    13.             isLocked = !isLocked;
    14.         }
    15.         Cursor.lockState = isLocked ? CursorLockMode.Locked : CursorLockMode.None;
    16.         Cursor.visible = !isLocked;
    17.     }
    18. }
     
    Nixel2013 likes this.
  3. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    Yes, thanks