C# 脚本延迟

C# 脚本延迟,c#,unity3d,C#,Unity3d,我有两个脚本,一个用于玩家移动,第二个用于他的力量,如果玩家按下“q”,他会使用他的力量,我会在第二个脚本中设置一个延迟,以防止他不停地按下。但整个游戏弗雷泽和我想知道,是否有一种方法可以让应用程序不被冻结,而只是在这个电源上增加冷却 代码如下: using UnityEngine; using System.Collections; public class SuperPower1 : MonoBehaviour { public Rigidbody rb; pu

我有两个脚本,一个用于玩家移动,第二个用于他的力量,如果玩家按下“q”,他会使用他的力量,我会在第二个脚本中设置一个延迟,以防止他不停地按下。但整个游戏弗雷泽和我想知道,是否有一种方法可以让应用程序不被冻结,而只是在这个电源上增加冷却

代码如下:

using UnityEngine;
using System.Collections;   

public class SuperPower1 : MonoBehaviour
{   
    public Rigidbody rb;
    public float forwardForce = 2000f;  // Variable that determines the forward force
    bool Break;
    void FixedUpdate()
    {   
        //Break
        {
            if (Input.GetKey("q"))
            {
                Break = true;
                int milliseconds = 2000;
                Thread.Sleep(milliseconds);
            }
            else
            {
                Break = false;
            }
        }
        if (Break == true)
        {
            rb.AddForce(0, 0, 500f * Time.deltaTime);
        }
        else
        {
            rb.AddForce(0, 0, forwardForce * Time.deltaTime);
        }
    }
}

Thread.sleep
决不是“我想延迟某事”的答案,因为
Thread.sleep
会导致程序无响应

如果你想推迟行动,你可以做三件事:

  • 创建一个计时器变量,并在
    Update()
    期间将其递增
    Time.deltaTime
    ,直到达到所需的持续时间
  • 使用tween引擎并在tween的
    onComplete
    处理程序中执行延迟代码

  • Thread.sleep
    决不是“我想延迟某事”的答案,因为
    Thread.sleep
    会导致程序无响应

    如果你想推迟行动,你可以做三件事:

  • 创建一个计时器变量,并在
    Update()
    期间将其递增
    Time.deltaTime
    ,直到达到所需的持续时间
  • 使用tween引擎并在tween的
    onComplete
    处理程序中执行延迟代码

  • 如@Draco18s所示,由于您的Unity游戏仅在一个线程中运行,
    thread.sleep
    将暂停整个游戏。下面是一个简单的脚本,您可以尝试。我还没有测试过,但你会明白的;)

    公共刚体rb;
    公共浮力=2000f;
    公共浮点数持续时间=2.0f;
    私有float endBonusTime=0;
    void FixedUpdate()
    {   
    if(endBonusTime=Time.Time)//不要放“else if”
    {
    rb.AddForce(0,0,500f*时间增量);
    }
    }
    

    如果要对此进行冷却,请尝试以下脚本:

    public Rigidbody rb;
    public float forwardForce = 2f;
    public float bonusDuration = 2.0f;   
    private float endBonusTime = 0 ;
    private float cooldown = 5.0f;
    
    void FixedUpdate()
    {   
        if ( endBonusTime < Time.time + cooldown && Input.GetKeyDown("q") )
        {
            endBonusTime = Time.time + bonusDuration ;
        }
        else if ( endBonusTime < Time.time )
        {
            rb.AddForce(0, 0, forwardForce * Time.deltaTime);
        }
        if (endBonusTime >= Time.time ) // Don't put "else if"
        {
            rb.AddForce(0, 0, 1 * Time.deltaTime);
        }
    }
    
    公共刚体rb;
    公共浮力=2f;
    公共浮点数持续时间=2.0f;
    私有float endBonusTime=0;
    专用浮子冷却时间=5.0f;
    void FixedUpdate()
    {   
    if(endBonusTime=Time.Time)//不要放“else if”
    {
    rb.AddForce(0,0,1*Time.deltaTime);
    }
    }
    
    如@Draco18s所示,由于您的Unity游戏仅在一个线程中运行,
    线程。睡眠将暂停整个游戏。下面是一个简单的脚本,您可以尝试。我还没有测试过,但你会明白的;)

    公共刚体rb;
    公共浮力=2000f;
    公共浮点数持续时间=2.0f;
    私有float endBonusTime=0;
    void FixedUpdate()
    {   
    if(endBonusTime=Time.Time)//不要放“else if”
    {
    rb.AddForce(0,0,500f*时间增量);
    }
    }
    

    如果要对此进行冷却,请尝试以下脚本:

    public Rigidbody rb;
    public float forwardForce = 2f;
    public float bonusDuration = 2.0f;   
    private float endBonusTime = 0 ;
    private float cooldown = 5.0f;
    
    void FixedUpdate()
    {   
        if ( endBonusTime < Time.time + cooldown && Input.GetKeyDown("q") )
        {
            endBonusTime = Time.time + bonusDuration ;
        }
        else if ( endBonusTime < Time.time )
        {
            rb.AddForce(0, 0, forwardForce * Time.deltaTime);
        }
        if (endBonusTime >= Time.time ) // Don't put "else if"
        {
            rb.AddForce(0, 0, 1 * Time.deltaTime);
        }
    }
    
    公共刚体rb;
    公共浮力=2f;
    公共浮点数持续时间=2.0f;
    私有float endBonusTime=0;
    专用浮子冷却时间=5.0f;
    void FixedUpdate()
    {   
    if(endBonusTime=Time.Time)//不要放“else if”
    {
    rb.AddForce(0,0,1*Time.deltaTime);
    }
    }
    
    我不知道
    Unity
    代码,因此可能需要对其进行一些转换,但在正常的
    C
    代码中,您可以使用一个变量来定义何时允许通电。这样,当用户使用电源时,您将该值更新为当前时间加上您希望等待的时间。然后,在授予功率之前,只需根据当前时间检查该变量:

    private float nextAllowedPowerUpTime = 0;
    
    void FixedUpdate()
    {
        if (Input.GetKey("q") && Time.time >= nextAllowedPowerUpTime)
        {
            rb.AddForce(0, 0, 500f * Time.deltaTime);
    
            // Set the next allowed power-up time to the current time plus two seconds
            nextAllowedPowerUpTime = Time.time + 2;
        }
        else
        {
            rb.AddForce(0, 0, forwardForce * Time.deltaTime);
        }
    }
    

    我不知道
    Unity
    code,因此可能需要对其进行一些转换,但在正常的
    C 35;
    code中,您可以使用一个变量来定义何时允许通电。这样,当用户使用电源时,您将该值更新为当前时间加上您希望等待的时间。然后,在授予功率之前,只需根据当前时间检查该变量:

    private float nextAllowedPowerUpTime = 0;
    
    void FixedUpdate()
    {
        if (Input.GetKey("q") && Time.time >= nextAllowedPowerUpTime)
        {
            rb.AddForce(0, 0, 500f * Time.deltaTime);
    
            // Set the next allowed power-up time to the current time plus two seconds
            nextAllowedPowerUpTime = Time.time + 2;
        }
        else
        {
            rb.AddForce(0, 0, forwardForce * Time.deltaTime);
        }
    }
    

    Thread.sleep不是为在Unity内部工作而设计的
    Er…不是<代码>线程。睡眠
    正是它的设计初衷,这种(不需要的)行为在任何单线程应用程序中都是相同的。我不得不告诉Minecraft modders停止使用Thread.sleep,原因完全相同:这不是它的设计目的。Unity可以生成子线程和
    线程。这些子线程中的sleep
    (正确使用)执行它设计的功能:暂停线程。
    Thread.sleep
    或Unity没有问题,但它的使用不正确。
    Thread.sleep或Unity没有问题,但它的使用不正确
    是的,你是对的,这就是我想说的谢谢你们现在我正试图阻止玩家使用它nonstop@David:检查我的更新答案。如果我的回答解决了你的问题,别忘了接受;)@谢谢你,你解决了我的问题。非常感谢你。我现在可以继续学习:)
    Thread.sleep是