unity disable keycode code example
Example 1: Disable key Unity
private System.Collections.Generic.Dictionnary<KeyCode,bool> keys = new System.Collections.Generic.Dictionnary<KeyCode,bool>() ; private Start() { keys.Add( KeyCode.S, true ) ; } private void Update() { if (Input.GetKeyDown(KeyCode.W)) { keys[KeyCode.S] = false ; } if (Input.GetKeyDown(KeyCode.S) && keys[KeyCode.S] ) { // Do something } }
Example 2: Disable key Unity
// At the top of your script using System.Collections.Generic; // In your class private Dictionary<KeyCode, bool> keys = new Dictionary<KeyCode, bool>(); private bool GetKeyDown( Keycode keyCode ) { if( !keys.ContainsKey( keyCode ) keys.Add( keyCode, true ); return Input.GetKeyDown( keyCode ) && keys[keyCode]; } // Do the same for GetKey and GetKeyUp private void DisableKey( KeyCode keyCode ) { if( !keys.ContainsKey( keyCode ) keys.Add( keyCode, false ); else keys[keyCode] = false; } private void EnableKey( KeyCode keyCode ) { if( !keys.ContainsKey( keyCode ) keys.Add( keyCode, true ); else keys[keycode] = true; }