让玩家在被击中时闪现
本文关键字:玩家 | 更新日期: 2023-09-27 18:10:01
我想让我的播放器在被击中时闪现。我有命中检测工作。他现在变成了红色。我想知道如何让真正的闪光发生。下面是处理被击中的代码片段:
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);
。我可以放几个这样的呼叫,在它们之间改变颜色,但我想知道是否有更简洁的方法。我不可能是第一个在游戏中制作flash内容的人。
编辑:我添加了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
}
}
现在的问题是闪电从未停止。它应该在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);
}
}
}
上面的答案是伟大的,但它并没有停止闪烁,因为它需要太长时间的浮动达到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);
}
}
}