Advertisement
SebastianLague

Custom input

Jul 28th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. class StickyInput {
  4.  
  5.     KeyCode keycode;
  6.  
  7.     // duration after key press that key should behave as if being repeatedly pressed
  8.     float stickyTime;
  9.     float stickyEndTime;
  10.  
  11.     public StickyInput(KeyCode keycode, float stickyTime) {
  12.         this.keycode = keycode;
  13.         this.stickyTime = stickyTime;
  14.     }
  15.  
  16.     public bool GetKeyDown() {
  17.         bool realKeyPress = Input.GetKeyDown(keycode);
  18.         if (realKeyPress) {
  19.             stickyEndTime = Time.time + stickyTime;
  20.         }
  21.  
  22.         return realKeyPress || Time.time < stickyEndTime;
  23.     }
  24.  
  25.     public bool GetKeyUp() {
  26.         return Input.GetKeyUp(keycode);
  27.     }
  28.  
  29.     public bool GetKey() {
  30.         return Input.GetKey(keycode);
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement