7

我试图让我的播放器在被击中时闪烁。我已经命中检测工作。他现在变成了红色。我想知道如何使实际的闪烁发生。这是一个处理被击中的片段:

if(otherObject.CompareTag("Minion")) //hit by minion
    {
        if(hitFromTop(otherObject.gameObject)) //if player jumped on enemy
        {
            otherObject.GetComponent<Minion>().setMoving(false); //stop moving
            playSound(KillEnemySound); //play killing enemy sound
            jump();
            Destroy(otherObject.gameObject); //kill minion

        }
        else //otherwise, enemy hit player
        {
            if(canTakeDamage)
            {
                changeColor(Color.red);
                loseHealth(1); //lose health
                canTakeDamage=false;
                yield return new WaitForSeconds(1.5f); //delay taking damage repeatedly
                changeColor(Color.white);
                canTakeDamage=true;
            }
            //indicate lost health somehow
        }

目前,通过yield return new WaitForSeconds(1.5f);. 我可以发出几个这样的调用,它们之间的颜色会发生变化,但我想知道是否有更简洁的方法。我不能成为第一个在游戏中做出闪光的人。

编辑:我添加了 Heisenbug 的 Flash() 代码如下:

IEnumerator FlashPlayer(float time, float intervalTime)
{
    canTakeDamage=false;
    float elapsedTime = 0f;
    int index = 0;

    while(elapsedTime < time )
    {
        Debug.Log("Elapsed time = " + elapsedTime + " < time = " + time + " deltaTime " + Time.deltaTime);
        mat.color = colors[index % 2];
        elapsedTime += Time.deltaTime;
        index++;
        yield return new WaitForSeconds(intervalTime);
    }
    canTakeDamage=true;
}

它在这里被调用:

void loseHealth(int damage)
{
    //irrelevant details omitted for brevity
            //the "if" checks if the player has run out of health, should not affect the else because this method is called from OnTriggerEnter...see below...
    else
    {
        StartCoroutine(FlashPlayer (1.5f, 0.5f));
    }
}

这是调用的地方(在 OnTriggerEnter 中):

    if(otherObject.CompareTag("Minion")) //hit by minion
    {
        if(hitFromTop(otherObject.gameObject)) //if player jumped on enemy
        {
            //irrelevant to the problem
        }
        else //otherwise, enemy hit player
        {
            if(canTakeDamage)
            {
                     loseHealth(1); 
            }
            //indicate lost health somehow
        }
    }

现在的问题是Flash永远不会停止。正如我所指定的,当 elapsedTime 达到 1.5f 时它应该停止,但是因为它只是增加少量(在 Time.deltaTime 中),所以它不会在很长一段时间内达到 1.5f。有没有我遗漏的东西可以让它更快地达到 1.5f,或者我可以选择另一个可以准确地用作停止点的数字?elapsedTime 的数字往往是:0、0.02、0.03693、0.054132 等,非常小,所以我不能从这里挑选。

4

2 回答 2

3

以下代码可能是您正在寻找的。time对象在改变其颜色的总量后应闪烁intervalTime

using UnityEngine;
using System.Collections;

public class FlashingObject : MonoBehaviour {

    private Material mat;
    private Color[] colors = {Color.yellow, Color.red};

    public void Awake()
    {

        mat = GetComponent<MeshRenderer>().material;

    }
    // Use this for initialization
    void Start () {
        StartCoroutine(Flash(5f, 0.05f));
    }

    IEnumerator Flash(float time, float intervalTime)
    {
        float elapsedTime = 0f;
        int index = 0;
        while(elapsedTime < time )
        {
            mat.color = colors[index % 2];

            elapsedTime += Time.deltaTime;
            index++;
            yield return new WaitForSeconds(intervalTime);
        }
    }

}
于 2013-04-19T22:57:48.540 回答
-1

上面的答案很好,但它并没有停止闪烁,因为浮子到达 5f 需要很长时间。

诀窍是将Flash函数中的第一个参数设置为更小的值,例如 1f,而不是 5f。获取此脚本,并将其附加到任何游戏对象。它将在黄色和红色之间闪烁约 2.5 秒,然后停止。

using UnityEngine;
using System.Collections;

public class FlashingObject : MonoBehaviour
{

    private Material _mat;
    private Color[]  _colors = { Color.yellow, Color.red };
    private float    _flashSpeed = 0.1f;
    private float _lengthOfTimeToFlash = 1f;

    public void Awake()
    {

        this._mat = GetComponent<MeshRenderer>().material;

    }
    // Use this for initialization
    void Start()
    {
        StartCoroutine(Flash(this._lengthOfTimeToFlash, this._flashSpeed));
    }

    IEnumerator Flash(float time, float intervalTime)
    {
        float elapsedTime = 0f;
        int index         = 0;
        while (elapsedTime < time)
        {
            _mat.color = _colors[index % 2];

            elapsedTime += Time.deltaTime;
            index++;
            yield return new WaitForSeconds(intervalTime);
        }
    }
}
于 2014-05-29T15:50:43.600 回答