I’m a bit embarrassed for asking this, but it seems all the previous question are quite old and not really solve what I’m trying to do.
As simple as it gets: I got a cube with a script attached to it. I need to know when the player clicks the cube, so I can do stuff in a separate function. I’m running some tests, as the last time I touched the input system was some years ago, and each time I click the cube I got 6 output messages (Which I assume correlates to 6 calls to my separate function).
Code:
private bool[] mousePressed = { false, false, false };
// Update is called once per frame
void Update () {
mousePressed[0] = Input.GetMouseButtonDown(0);
mousePressed[1] = Input.GetMouseButtonDown(1);
mousePressed[2] = Input.GetMouseButtonDown(2);
doStuff();
}
private void doStuff() {
if (mousePressed[0] || mousePressed[1] || mousePressed[2]) {
clickTime = Time.time;
Debug.Log("Click!");
mousePressed[0] = mousePressed[1] = mousePressed[2] = false;
}
}
I know where the problem comes, as Update() is called once per frame, thus I got 4-6 calls to the function (and then to doStuff()) per frame. So I added the line to reset the mousePressed array:
mousePressed[0] = mousePressed[1] = mousePressed[2] = false;
But it’s ignoring me, as I keep getting all the calls per frame.
And I’m clueless. I know I can use
OnMouseDown()
but it only detects left clicks (and I need both left and right clicks, and also this is somewhat personal now).
So, any help here?