2016-09-18 80 views
1

我目前正在研究一个具有精灵效果的游戏。玩家有能力在一段时间内改变他们的“冰刀”。 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

检查,如果他们是平等的,你应该写这样的

if (Blade1.gameObject.GetComponent<SpriteRenderer>().sprite == IceBlade) 

timer -= Time.deltaTime; 

只会发生一次,当other.gameObject.CompareTag("IcePickup")是真的 和这里

timer -= Time.deltaTime; 
     timer ++; 

你降低定时器的值,同时增加它

所以这显然不是一个计时器。做一个计时器timer -= Time.deltaTime;必须Update被称为是能算,也可以使用coroutinesherehere是实现双向

+0

OK揭掉,但计时器仍然不起作用 –