【发布时间】:2021-09-20 07:03:18
【问题描述】:
我的平台游戏有一个默认的“未受伤”时间,我的玩家从受伤状态过渡到正常状态;我认为正常时间是 0.54 秒。但是对于特定对象,我希望不受伤害的时间更短。我通过将它添加到我未受伤的 IEnumerator 来做到这一点
IEnumerator Unhurt(float? time2)
{
time2 = 4;
SoundMan1.PlayHurtSound();
// SoundMan1.PlayerHurt3.PlayOneShot(SoundMan1.PlayrHurtClips3[Random.Range(0, 3)]);
Debug.Log("Unhurting!");
if(time2 == null)
{
} if(time2 > .02f && time2!=null)
{
yield return new WaitForSeconds(time2);
}
else
{
yield return new WaitForSeconds(unhurttime);
}
IsHurt = false;
rb.constraints = RigidbodyConstraints2D.None;
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
Debug.Log("Unhurt");
}
在我的尖峰脚本中(我从中调用这个协程的脚本。)这是调用它的代码..
collision.gameObject.GetComponent<Player>().StartCoroutine("Unhurt");
我想要这样,如果我在“Unhurt”之后不写任何东西,那么 time2 会浮动吗?将为空,它只会等待默认持续时间。问题是 WaitForSeconds(time2);有错误。它说不能转换浮动?浮动。
如果我在“Unhurt”之后添加 .2f 或其他任何内容并将其作为 time2 传递,我希望浮点数不为空,然后它将等待新的持续时间。
【问题讨论】:
-
你应该使用
time2.HasValue来检查它是否有一个值,一旦你确定它有一个值,你应该使用time2.Value来检查它。由于您在函数的第一行为其分配了一个值,因此它将始终具有一个值。 docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
标签: c# unity3d ienumerator