命中时播放器闪光

时间:2013-04-19 22:23:02

标签: 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等非常小,所以我无法从这里找到任何东西。

2 个答案:

答案 0 :(得分: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);
        }
    }

}

答案 1 :(得分:-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);
        }
    }
}