c# - 击中时让玩家闪现

标签 c# unity3d

我想让我的播放器在被击中时闪烁。我有命中检测工作。他目前变为红色。我想知道如何使实际闪烁发生。这是处理被击中的片段:

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

这是调用 THAT 的地方(在 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 等。非常小,所以我无法从这里挑选任何东西。

最佳答案

以下代码可能是您要查找的内容。在 intervalTime

之后,对象应该闪烁 time 改变其颜色的总量
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);
        }
    }

}

关于c# - 击中时让玩家闪现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16114349/

相关文章:

c# - 在 Windows Phone 8 上的 Unity 中发出发布请求

c# - 带有并排选项卡的 Unity 自定义窗口

c# - App.Config 未更新

c# - Windows 8 future 访问列表中的内容持续多长时间

c# - 混淆+反射

java - Unity 是否使用 Xamarin?

c# - Unity3d 点击并拖动 Gizmos

java - 对函数的澄清 - 它是一种属性、特殊代码、过程/例程的另一种说法还是有多个定义?

c# - Microsoft Graph API 访问 token

c# - Unity C# 脚本中未调用重写虚函数