我目前正在开发一款带有精灵效果的游戏。玩家有能力在一段时间内改变他们的“冰刀”。 2秒后,我希望效果恢复到正常的“冰刀”。因此,我需要在Unity中使用计时器。我试了很长时间,但却无法完成这项工作。
以下是我的示例代码:
if (other.gameObject.CompareTag("IcePickup"))
{
Destroy(other.gameObject);
timer -= Time.deltaTime;
timer ++;
Blade1 = GameObject.Find("Blade1");
Blade1.gameObject.GetComponent<SpriteRenderer>().sprite = IceBlade;
if (Blade1.gameObject.GetComponent<SpriteRenderer>().sprite = IceBlade)
{
//i put a timer here but i cant figure out why is isnt working and print isnt showing.
if (timer > 2)
{
Blade1.gameObject.GetComponent<SpriteRenderer>().sprite = BladeNormal;
print("working");
}
}
}
答案 0 :(得分:0)
检查它们是否相等,你应该像这样写
if (Blade1.gameObject.GetComponent<SpriteRenderer>().sprite == IceBlade)
和
timer -= Time.deltaTime;
仅在other.gameObject.CompareTag("IcePickup")
为真时发生一次
这里是
timer -= Time.deltaTime;
timer ++;
你正在减少定时器值,同时增加它
所以这显然不是一个计时器。要使计时器timer -= Time.deltaTime;
必须在Update
中调用才能计算,您也可以使用coroutines
,here和here两种方式的实现