简体   繁体   中英

When input is pressed x times do something

I'm currently working on a game in Unity and am unable to get something done. I want it so that when the input is pressed x times (A melee attack) the character stopt working until You pressed another button for x times ie 10 times. The player should be able to attack ie 3 times, but when he does this the character enters a "fake death" state where he is not able to walk or melee attack with the player anymore. At that time the player should hit another key for 10 times and then the player will be able to walk en melee attack again. I thought I could realize this with a simple if and else statement, but didn't get it working thus far. For some reason my else part is executed immediately instead of after using the melee attack for 5 times.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeleeCounter : MonoBehaviour {

    public int attackNumber = 0;

    public GameObject meleeHitbox;

    // Update is called once per frame
    void Update () {
        if (attackNumber < 5 && Input.GetButtonDown ("Fire3"))
        {
            attackNumber++; // increment the counter
            meleeHitbox.gameObject.SetActive (true);
            Debug.Log ("Attack");
        }
        if (Input.GetButtonUp ("Fire3")) {
            meleeHitbox.gameObject.SetActive (false);
        }
        else
        {
            GetComponent<PlayerController>().enabled = false;
            Debug.Log ("Too many attacks");
            // Here should come a script that if i.e. Fire4 is pressed 10 times reset attackNumer to 0; and set PlayerController to true.
        }
    }
}

It seems you've mixed up your conditions somewhat. As it's currently written, your code will execute the else block whenever Input.GetButtonUp ("Fire3") is false (aka. Whenever Fire3 has not just been released), regardless of how many times the player has attacked; the two if statements you've written are independent of each other.

The else statement should really be attached to the attackNumber , not the result of Input.GetButtonUp ("Fire3") . Additionally, you may want to disable the player script right after the attack is made, once the attackNumber has been updated.

Here's the code shuffled around a little, which should be closer to what you want to accomplish:

void Update () {
    // Only bother checking for Fire3 if attacks can still be made
    if (attackNumber < 5)
    {
        if (Input.GetButtonDown ("Fire3"))
        {
            attackNumber++; // increment the counter
            meleeHitbox.gameObject.SetActive (true);
            Debug.Log ("Attack");

            // Detect when too many attacks are made only if an attack was just made
            if (attackNumber == 5) {
                GetComponent<PlayerController>().enabled = false;
                Debug.Log ("Too many attacks");
            }
        }
    }
    // If attacks can't be made, then check for Fire4 presses
    else
    {
        // Here should come a script that if i.e. Fire4 is pressed 10 times reset attackNumer to 0; and set PlayerController to true.
    }

    // Allow disabling of the hitbox regardless of whether attacks can be made, so it isn't left active until after the player is enabled again
    if (Input.GetButtonUp ("Fire3")) {
        meleeHitbox.gameObject.SetActive (false);
    }
}

Hope this helps! Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM