2016-06-14 17 views
0

凝視對象時我有這個命中信息。當注視時,物體出現,但當視線離開時消失。我希望該對象在查看10秒後顯示。我在正確的道路上嗎?顯示凝視統一對象

float timeLeft = 10.0f; 
timeLeft -= Time.deltaTime; 
if (Hit == InfoCube) 
{ 
    GameObject.Find("GUIv2").transform.localScale = new Vector3(0.02f, 0.2f, 0.8f) // Shows object 
} 
else if (timeLeft < 0) 
{ 
    GameObject.Find("GUIv2").transform.localScale = new Vector3(0, 0, 0); 
    GameObject.Find("GUIv2").SetActive(false);      
} 

回答

1

此代碼位於何處?我假設它在Update()方法中。

無論如何,我可以看到代碼的一些問題。這裏是我的建議:

  1. 緩存「GUIv2」對象,所以你不必每幀「找到」它。
  2. 您不需要更改localScale值。只需使用SetActive。
  3. 不要每次初始化變量timeLeft。它永遠不會達到0

下面是一些示例代碼,應該實現你在找什麼:

float timeLeft = 10f; 
    bool isGazed; 
    GameObject GUIv2; 
    void Start() 
    { 
     GUIv2 = GameObject.Find("GUIv2"); 
    } 

    void Update() 
    { 
     if (Hit == InfoCube) 
     { 
      if (!isGazed) 
      { 
       // first frame where gaze was detected. 
       isGazed = true; 
       GUIv2.SetActive(true); 
      } 
      // Gaze on object. 
      return; 
     } 
     if (Hit != InfoCube && isGazed) 
     { 
      // first frame where gaze was lost. 
      isGazed = false; 
      timeLeft = 10f; 
      return; 
     } 

     timeLeft -= Time.deltaTime; 
     if (timeLeft < 0) 
     { 
      GUIv2.SetActive(false); 
     } 
    } 
+0

真棒的感謝! – Wzrd