命中时播放器闪光

mut*_*y91 7 c# unity-game-engine

我试图让我的播放器在被击中时闪光.我已经点击检测工作了.他目前变红了.我想知道如何实现闪烁.这是一个处理被击中的片段:

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
        }
Run Code Online (Sandbox Code Playgroud)

目前,通过改变为红色是恒定的(不闪烁)和暂时的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;
}
Run Code Online (Sandbox Code Playgroud)

它在这里被称为:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是调用的地方(在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
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

Hei*_*bug 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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)