unity3d 防止玩家在C#中发送密钥垃圾邮件(Unity)

cxfofazt  于 2022-11-15  发布在  C#
关注(0)|答案(6)|浏览(187)

我目前正在做教程学习从统一的代码,在这一节有奖金的挑战,这并不能帮助你解决它。它说,我必须防止球员从垃圾邮件空格键产卵狗。我是新的C#,我开始在网上寻找,但我看到一些关于协同例程,我仍然不知道这是什么,有一个简单的方法来做到这一点,我在网上搜索发现了类似的东西,但是我不能让它工作。我也试着做一些条件,比如canSpawn,但是我不知道如何很好地实现它,Unity给了我一个错误,我不能在bool和keypress事件之间使用&&

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);
    }
}
}
puruo6ea

puruo6ea1#

你已经很接近了。有一件事可能会让你更容易理解逻辑,那就是只向上计数,而不是试图向下计数。所以,在你的例子中,代码看起来像这样:

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时间(2.0f)才能再次按下空格键。

wkftcu5l

wkftcu5l2#

我正在用我的手机。如果我犯了一些语法错误,我很抱歉。

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;
}
pkln4tw6

pkln4tw63#

这是我基于秒表的解决方案:

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);
    }
}
eit6fx6z

eit6fx6z4#

我用了协程来完成这个任务,它的代码有点多,但是它工作得很好。

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);
    }
}
stszievb

stszievb5#

另一个例子只是为了好玩

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);
    }

}
jaql4c8m

jaql4c8m6#

下面的代码是我用的,因为它很短很好用。

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);
}

}

相关问题