diff --git a/Assets/Scripts/ButtonToKey.cs b/Assets/Scripts/ButtonToKey.cs index f6ce7f3..b55c030 100644 --- a/Assets/Scripts/ButtonToKey.cs +++ b/Assets/Scripts/ButtonToKey.cs @@ -10,14 +10,30 @@ public class ButtonToKey : MonoBehaviour static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); public VirtualKeyCode keyToPress; + private int _insideColliderCount = 0; + + private void Update() + { + if (_insideColliderCount == 0) + { + keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 2, UIntPtr.Zero); + } + else + { + keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 0, UIntPtr.Zero); + } + } + private void OnTriggerEnter(Collider other) { - keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 0, UIntPtr.Zero); + _insideColliderCount += 1; + } private void OnTriggerExit(Collider other) { - keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 2, UIntPtr.Zero); + _insideColliderCount -= 1; + _insideColliderCount = Mathf.Max(0, _insideColliderCount); } }