Home>

unity uses C #.
It is a 2D game.
I would like to do different processing depending on the game situation using if or else if,
Under the current situation, everything written in if and else if will be executed at once.


① Pressing the space key while the image is in contact with the player (first time) will raise the image slightly.
(2) Pressing the space key again while in contact (second time) raises the image slightly.
③ If i press the space key again while in contact (third time), the image disappears.

At present, ① ~ ③ will be executed at once if you press the space key once.

I think this may be due to an elementary mistake/misunderstanding, but I would appreciate any advice.

Error message

No error has occurred.

Applicable source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pull: MonoBehaviour {
    private bool flag = false;
    private bool spaceonce = false;
    private bool spacetwice = false;
Void OnTriggerStay2D (Collider2D col) {
   If (col.gameObject.tag == "player") {
            if (Input.GetKeyDown (KeyCode.Space)) {
                // The following ①-③ will be executed at once.
                if (spaceonce == false&&spacetwice == false) {// 1
                    transform.Translate (0.0f, 0.1f, 0.0f);
                    spaceonce = true;
                }
                else if (spaceonce == true&&spacetwice == false) {// ②
                    transform.Translate (0.0f, 0.1f, 0.0f);
                    spacetwice = true;
                }
                else if (spaceonce == true&&spacetwice == true) {// ③
                    flag = true;
                }
           }
   }
 }
*
Void Update () {
    if (flag) {
           Destroy (this.gameObject);
    }
  }
}

I tried to write a part of the process in Update, but it became the same behavior.

Supplemental information (FW/tool version etc.)

We are using Unity 2019.3.0a8 (64-bit).

  • Answer # 1

    Let's bring

    if (Input.GetKeyDown (KeyCode.Space)) {~}into Update.


    ■ Why does this work?
    Input.GetKeyDownis "true only for one frame during which there was key input".
    This one frame is an "Update interval (variable)" unit, that is, one update.
    It works normally because of the following movement.

    Key input is performed first,Input.GetKeyDownbecomes true and enters the if statement in ①.

    Input.GetKeyDownbecomes false at the next update (regardless of whether the key is actually pressed).

    Next,Input.GetKeyDownbecomes true when the player releases the key and then presses it again.

    The same applies to

    ③.

    OnTriggerStay2D operates at "FixedUpdate interval (fixed)".
    Depending on the frame rate (fps) of the update,FixedUpdates may be executed multiple times before the next update comes.
    (For example, if Update is at 0.1 second intervals and FixedUpdate is at 0.02 second intervals, 5 FixedUpdates will be executed before 1 Update is executed)
    If this happens, the following behavior will be taken, so it will appear that (1) to (3) are being executed together.

    Key input is performed first,Input.GetKeyDownbecomes true and enters the if statement in ①.

    Input.GetKeyDownremains true even in the next OnTriggerStay2D (because the next update has not arrived,Input.GetKeyDowndoes not become false) Enter the if statement.

    The same applies to

    ③.

  • Answer # 2

    You can see that spaceonce is true if you do something, but there is no place to make it false, so
    While you hold down the key, it will continue to run.