簡體   English   中英

防止玩家在 C# (Unity) 中發送垃圾郵件

[英]Prevent a player from spamming a key in C# (Unity)

我目前正在做教程從 unity 學習代碼,在本節中有額外的挑戰,不能幫助你解決它。 它說我必須防止玩家亂用空格鍵來生成狗。 我是 C# 的新手,我開始在網上尋找,但我看到了一些關於 CoRoutines 的信息,但我仍然不知道那是什么,有沒有一種簡單的方法可以做到這一點,在網上搜索我發現了類似的東西,但我無法讓它工作。 我也嘗試做一些像 canSpawn 這樣的條件,但我不知道如何很好地實現它,Unity 給我一個錯誤,我不能在 bool 和按鍵事件之間使用 &&

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

public class PlayerControllerX : MonoBehaviour
{
    public GameObject dogPrefab;
    public float time = 2.0f;
    public float timer = Time.time;

    // Update is called once per frame
    void Update()
    {
        timer -= Time.deltaTime;
        if (timer > time)
        {
            // On spacebar press, send dog
            if (Input.GetKeyDown(KeyCode.Space))
            {
                spawnDog();
            }
            timer = time;
    }

    void spawnDog()
    {
            Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
    }
}

你很接近。 可以更容易理解邏輯的一件事是,只是向上計數而不是嘗試向下計數。 因此,在您的情況下,代碼如下所示:

        void Update ( )
        {
            timer += Time.deltaTime;
            if ( timer >= time )
            {
                // On spacebar press, send dog
                if ( Input.GetKeyDown ( KeyCode.Space ) )
                {
                    spawnDog ( );
                    timer = 0;
                }
            }
        }

        void spawnDog ( )
        {
            Instantiate ( dogPrefab, transform.position, dogPrefab.transform.rotation );
        }

timer不斷增加,當它大於您的time值 (2.0f) 時,它允許您按下一個鍵。 如果隨后按下某個鍵,則timer將重置為 0,玩家需要等待time time (2.0f) 才能再次按下空格鍵。

我正在使用我的手機。 如果我犯了一些語法錯誤,我很抱歉。

bool isReadyForInstantiate;

void Start(){
    isReadyForInstantiate = true;
}

void Update(){
    if(isReadyForInstantiate && Input.GetKeyDown(KeyCode.Space)){
        StartCoroutine(PreventSpam());
        Instantiate(dogPrefab, transform.position, Quaternion.identity);
    }
}

IEnumerator PreventSpam(){
    isReadyForInstantiate = false;
    yield return new WaitForSeconds(2);
    isReadyForInstantiate = true;
}

這是我基於秒表的解決方案:

using UnityEngine;
using System.Diagnostics; // hides UnityEngine.Debug. if needed use qualified call

public class PlayerControllerX : MonoBehaviour
{
    public GameObject dogPrefab;
    public double dogDelayMillis = 2000d;

    private Stopwatch stopWatch;

    private void Start()
    {
        stopWatch = new Stopwatch();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (stopWatch.IsRunning)
            {
                if (stopWatch.Elapsed.TotalMilliseconds > dogDelayMillis)
                {
                    stopWatch.Reset();
                    SpawnDog();
                }
            }
            else
            {
                SpawnDog();
            }
        }
    }

    private void SpawnDog()
    {
        stopWatch.Start();
        Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
    }
}

我在這個任務中使用了 Coroutines,它有更多的代碼,但效果很好。

public class PlayerControllerX : MonoBehaviour{
    public GameObject dogPrefab;
    private bool isCoolDown = false;
    private float coolDown = 1f;

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (isCoolDown == false)
            {
                SpawnDog();
                StartCoroutine(CoolDown());
            }
        }
    }

    IEnumerator CoolDown()
    {
        isCoolDown = true;
        yield return new WaitForSeconds(coolDown);
        isCoolDown = false;
    }

    private void SpawnDog()
    {
         Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
    }
}       

另一個例子只是為了好玩

    public GameObject dogPrefab;
    [Range(0f,2f)]
    private float timer = 1.0f;
    private float waitTime = 1.0f;

    // Update is called once per frame
    void Update()
    {
        // Delay Input Timer - only execute the spacebar command if timer has caught up with waitTime
        if (timer < waitTime)
        {}
        // On spacebar press, send dog
        else if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
            // Resets Timer
            timer = 0;
        }
        // Run Timer every frame
        timer += Time.deltaTime;
        Debug.Log(timer);
    }


}

我被困在同樣的事情上,謝謝。 下面的代碼是我使用的,因為它短小精悍。

public GameObject dogPrefab;
[Range(0f,2f)]
private float timer = 1.0f;
private float waitTime = 1.0f;

// Update is called once per frame
void Update()
{
    // Delay Input Timer - only execute the spacebar command if timer has caught up with waitTime
    if (timer < waitTime)
    {}
    // On spacebar press, send dog
    else if (Input.GetKeyDown(KeyCode.Space))
    {
        Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
        // Resets Timer
        timer = 0;
    }
    // Run Timer every frame
    timer += Time.deltaTime;
    Debug.Log(timer);
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM