Unity Input detected multiple times in a press

前端 未结 2 796
攒了一身酷
攒了一身酷 2021-01-15 15:18

I have a source MonoBehavior script with these methods:

private void Update () {
    if (Input.GetMouseButton(0)) {
        HandleInput();
    }         


        
相关标签:
2条回答
  • 2021-01-15 16:04

    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.

    0 讨论(0)
  • 2021-01-15 16:06

    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.

    0 讨论(0)
提交回复
热议问题