I have a source MonoBehavior
script with these methods:
private void Update () {
if (Input.GetMouseButton(0)) {
HandleInput();
}
For me, I had placed the Input.GetMouseButtonDown(0) inside FixedUpdate. Because of this, Input.GetMouseButtonDown(0) returns true multiple times in a single mouse click. Replace FixedUpdate with Update, and it will work.
This is a problem almost all new Unity programmers go through.
Input.GetMouseButton is true
every frame when the mouse
Button is held down until released. Again, it will be true
every
frame until released.
Input.GetMouseButtonDown is true
just once when the
mouse button held down. It will not be true
again until mouse is
released and pressed again.
In your case, it looks like you need Input.GetMouseButtonDown which will only be true once and can only be true again when released and pressed again.
The-same thing applies to GetKey
, GetKeyDown
and GetKeyUp
.
Note:
If you use Input.GetMouseButtonDown but it is still being called multiple times then make sure that your script is not attached to another or multiple GameObjects.