Input.GetMouseButton error on low frame rapid input,Input.GetMouseButton error on LOW FRAME RAPIDINPUT
I made input getter component to use on UI
Code below seems to work fine but it makes error if LOW FRAME AND FAST REPEATED MOUSE CLICK
If mouse is double clicked rapidly, (making keydown->keyup->keydown->keyup in real mouse) functions are called in this sequence rarely.
PcInputEnter()
PcInputExit()
PcInputEnter()
and nothing happens
This is code I used.
public class TouchTriggerKeyboardBinding : MonoBehaviour
{
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN // PC
[SerializeField] private KeyCode keyCode;
private TouchTrigger touchTrigger;
private bool pressCach;
private void Start()
{
touchTrigger = GetComponent<TouchTrigger>();
}
private bool CheckInput() => Cursor.lockState == CursorLockMode.Locked && Input.GetMouseButton(mouseIndex);
private void Update()
{
bool pressNew = CheckInput();
if (pressNew != pressCach)
{
pressCach = pressNew;
if (pressNew)
touchTrigger.PcInputEnter();
else
touchTrigger.PcInputExit();
}
}
#endif
}
This error is not easy to make, but it occurs once per 20 minutes in average, and our game is action game. this bug can not be ignored.
This error occurs many time on editor, more than 5 team member experienced this problem in editor mode play. (but hard to make occur again intentionally)
To make this error intentionally, recommended setting is like this.
make editor slow. lower than 15frame/sec with many objects and [Scene] tab
we generate heavy object when mouse click, but this bug occurs without it too.
This is test code I made. if bug explained occur, [pressCach] will be true even you are not pressing mouse.
Any solution will be welcomed.
public class GetButtonDownTest : MonoBehaviour
{
public int sum;
public KeyCode keycode;
public int mouseButtonIndex = 0;
public bool pressCach = false;
bool CheckInput()
{
if (keycode != KeyCode.None)
return Input.GetKey(KeyCode.A);
else
return Input.GetMouseButton(mouseButtonIndex);
}
bool CheckDown()
{
if (keycode != KeyCode.None)
return Input.GetKeyDown(KeyCode.A);
else
return Input.GetMouseButtonDown(mouseButtonIndex);
}
bool CheckUp()
{
if (keycode != KeyCode.None)
return Input.GetKeyUp(KeyCode.A);
else
return Input.GetMouseButtonUp(mouseButtonIndex);
}
// Update is called once per frame
void Update()
{
bool isPress = CheckInput();
if(CheckDown())
Debug.Log("Down" + keycode);
if(CheckUp())
Debug.Log("Up"+ keycode);
if (pressCach != isPress)
{
pressCach = isPress;
if (isPress)
{
sum++;
Debug.Log("++"+ keycode);
}
else
{
sum--;
Debug.Log("--"+ keycode);
}
}
}
},