【问题标题】:WaitForSeconds isn't workingWaitForSeconds 不起作用
【发布时间】:2015-12-20 00:29:44
【问题描述】:

我有一门课程正在改变玩家的速度。这是代码:

using UnityEngine;
using System.Collections;

public class MultiplySpeed : MonoBehaviour {
    public int multiplier = 2;
    public Controls player;
    bool flag = false;
    void OnTriggerExit(Collider c){
        if(c.tag == "Player"){
            player = c.gameObject.GetComponent<Controls>();
            if(!flag){
                multiplySpeed();
                StartCoroutine(powerUp());
            }
            flag = true;
        }
    }
    public IEnumerator powerUp(){
        yield return new WaitForSeconds(10);
        backToNormal ();
        StopCoroutine(powerUp ());
    }
    public void multiplySpeed(){
        player.speed = player.speed * multiplier;
    }
    public void backToNormal(){
        player.speed = player.speed/ multiplier;
    }
}

我有两个问题,第一个是启动协程并且flag变量设置为true后,再次调用方法OnTriggerExit,flag为false,一切都重新执行。第二个问题是 backToNormal 方法似乎从未执行过,它只是使速度倍增,并且永远不会恢复正常。任何帮助表示赞赏。

编辑: 在尝试了 Lasse Klüver 建议的方法后,这是我的代码:

using UnityEngine;
using System.Collections;

public class MultiplySpeed : MonoBehaviour {
    public int multiplier = 2;
    public Controls player;
    bool flag = false;
    float timer = 5.0f;
    void update(){
        timer -= Time.deltaTime;
        if (timer == 0) {
            backToNormal();
            timer = -1f;
        }

    }
    void OnTriggerExit(Collider c){
        if(c.tag == "Player"){
            player = c.gameObject.GetComponent<Controls>();
            if(!flag){
                timer = 5.0f;
                flag = true;
                multiplySpeed();
            }
        }
    }
    public void multiplySpeed(){
        player.speed = player.speed * multiplier;
    }
    public void backToNormal(){
        player.speed = player.speed/ multiplier;
    }
}

但是我也有同样的问题。

【问题讨论】:

标签: c# unity3d coroutine


【解决方案1】:

尝试使用这个脚本:

using UnityEngine;
using System.Collections;

using UnityEngine;
using System.Collections;

public class MultiplySpeed : MonoBehaviour
{
    public int multiplier = 2;
    public Controls player;
    bool flag = false;
    float timer = 5.0f;
    bool countDown = false;

    void update()
    {
        if (countDown)
        {
            timer -= Time.deltaTime;
            if (timer <= 0)
            {
                backToNormal();
                countDown = false;
            }
        }

    }
    void OnTriggerExit(Collider c)
    {
        if (c.tag == "Player")
        {
            player = c.gameObject.GetComponent<Controls>();
            if (!flag)
            {
                countDown = true;
                timer = 5.0f;
                flag = true;
                multiplySpeed();
            }
        }
    }
    public void multiplySpeed()
    {
        player.speed = player.speed * multiplier;
    }
    public void backToNormal()
    {
        player.speed = player.speed / multiplier;
    }
}

【讨论】:

  • 我不想数帧数,我要数秒数。
  • 我在答案中添加了一个示例。在这种情况下,计数器变量将在 3 秒后为 0。
  • 我遇到的问题和以前一模一样,我已经用我的新代码更新了问题。
【解决方案2】:

如果你想在给定的时间(5 秒)内改变玩家的速度,如果它击中了加电,我会反过来。我不会在加电内部做计时的东西——因为我认为它应该在玩家击中它后立即被摧毁——我会通过附加到玩家的脚本来做。 像下面这样的东西应该可以工作:

using UnityEngine;

public class SpeedBoostPowerUp : MonoBehaviour
{
    private void OnTriggerEnter(Collider c)
    {
        if(c.tag == "Player")
        {
            Destroy(gameObject);
        }
    }
}

然后您可以在移动组件 (Controls.cs) 中添加速度提升代码。你可以做这样的事情(我不想改变太多你的代码):

// INSIDE CONTROLS COMPONENT
private void OnTriggerEnter(Collider c)
{
    if(c.tag == "SpeedBoostPowerUp")
    {
        if (!flag)
        {
            StartCorouting(BoostSpeed);
        }
    }
}

public void MultiplySpeed()
{
    this.speed = this.speed * this.multiplier;
}

public void BackToNormal()
{
    this.speed = this.speed / this.multiplier;
}

private IEnumerator BoostSpeed()
{
    flag = true;
    MultiplySpeed();
    yield return new WaitForSeconds(5);
    BackToNormal();
    flag = false;
}

当然,您需要使用“SpeedBoostPowerUp”标签来标记您的加电游戏对象。使用该标志,因此您不能一次获得多个速度提升。 如果您使用“静态”加电装置,这应该可以正常工作 - 因此具有固定行为的加电装置。如果你想给玩家一个随机的提升等,如果它击中了力量——比如在马里奥卡丁车中——我会以不同的方式来构建它。但这对于您想要实现的目标来说太复杂了。

注意:我不会通过 speed = speed / multiplier 恢复正常速度,而是在乘以之前使用一个变量来存储速度,因为它应该更不容易出错。像这样:

private float speedBeforeBoost;

public void MultiplySpeed()
{
    this.speedBeforeBoost = this.speed;
    this.speed *= this.multiplier;
}

public void BackToNormal()
{
    this.speed = this.speedBeforeBoost;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2015-06-14
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 2019-12-15
    相关资源
    最近更新 更多