2017-04-10 20 views
4

我一直在這個簡單的問題中陷入困境,但無法理解爲什麼我無法控制它。在玩家面前總是顯示物體

我有這些線中的哪一個在播放器的某些旋轉,並顯示在我的播放器(在代碼camRotationToWatch對象名)的前我的畫布對象的代碼。

if (camRotationToWatch.transform.localEulerAngles.x >= navigationCanvasXMinmumLimit && camRotationToWatch.transform.localEulerAngles.x <= navigationCanvasXMaximumLimit) 
     { 
      if (!navCanvasHasDisplay) 
      { 

       navigationCanvas.SetActive(true); 
       //Debug.Log(camRotationToWatch.transform.forward); 
       Vector3 navCanvas = camRotationToWatch.transform.position + camRotationToWatch.transform.forward * navCanvasDisplayDistanceFromCam; 
       navCanvas = new Vector3(navCanvas.x, 2f, navCanvas.z); 
       navigationCanvas.transform.position = new Vector3(navCanvas.x, navCanvas.y, navCanvas.z); 

       navigationCanvas.transform.rotation = camRotationToWatch.transform.rotation; 

       navCanvasHasDisplay = true; 

      } 
     } 

     else 
     { 
      //navigationCanvas.SetActive(false); 
      if (locationPanel.activeSelf == false && infoPanel.activeSelf == false) { 
       navigationCanvas.SetActive(false); 
       navCanvasHasDisplay = false; 
      } 
     } 

此代碼實際上是正常工作時camRotationToWatch對象從旋轉下往上和Canvas顯示在正確的位置,但我試圖從旋轉camRotationToWatch升轉跌它顯示(主動)帆布在非常高的位置。我怎樣才能限制畫布顯示在相同的位置(無論玩家從上到下還是向下旋轉到),但顯示在玩家對象的前面?

+0

您是否可以將畫布父母放到播放器中並忘記腳本? – Absinthe

+0

我可以但它也會上下左右轉動這是不正確 –

+0

@Absinthe我會根據相機旋轉,我真的不想做 –

回答

0

這可能不是一個完整的答案,但可能有所幫助。此行:

Vector3 navCanvas = camRotationToWatch.transform.position + camRotationToWatch.transform.forward * navCanvasDisplayDistanceFromCam; 

您正在創建一個固定距離的位置,從camRotationToWatch。但是,如果該物體向上或向下看,該位置不是水平navCanvasDisplayDistanceFromCam。如果它直線上升,那麼這個位置實際上就在上面。

所以,當你這樣做是爲了設置一個固定的垂直高度:

navCanvas = new Vector3(navCanvas.x, 2f, navCanvas.z); 

你不是從camRotationToWatch,你認爲你所得到的距離。 而不是使用camRotationToWatch.transform.forward,從它創建一個向量,並將Y分量歸零,並在使用它來偏移位置之前進行歸一化。 (雖然你需要注意零長度的矢量)。 無論是否解決您的問題,這是很難猜測,但它應該有助於改善你的結果。

編輯:這裏是你如何避免與畫布太接近問題的例子:

Vector3 camForward = camRotationToWatch.transform.forward; 
camForward.y = 0; 
if (camForward.magnitude == 0) 
{ 
    //TODO: you'll need to decide how to deal with a straight up or straight down vector 
} 
camForward.Normalize(); 
//Note: now you have a vector lying on the horizontal plane, pointing in 
//the direction of camRotationToWatch 
Vector3 navCanvas = camRotationToWatch.transform.position + camForward * 
    navCanvasDisplayDistanceFromCam; 
//Note: if you want the canvas to be at the player's height (or some offset from), 
//use the player's y 
navCanvas = new Vector3(navCanvas.x, Player.transform.y, navCanvas.z); 
navigationCanvas.transform.position = navCanvas; 

同樣,這可能不會解決你所有的問題,但將有助於確保你的畫布在於在設定距離水平從玩家,也將補償玩家的上下運動。

+0

我該如何前進玩家?這只是做這個工作的方式 –

+0

其實我無法修正高度,因爲我的球員我的球員位置正在改變上下也 –

+0

我已經編輯了我的答案給一個例子如何運作的一些意見。希望這有助於更好地理解。 – avariant

2

有點努力想弄清楚你到底想幹什麼。但是,這做了我想你在哪裏試圖做

public GameObject follow; // The object you want to rotate around 
public float distance = 2; // Distance to keep from object 

private void Update() { 
    Vector3 forward = follow.transform.forward; 
    forward.y = 0; // This will result in Vector3.Zero if looking straight up or down. Carefull 

    transform.position = follow.transform.position + forward * distance; 
    transform.rotation = Quaternion.LookRotation(forward, Vector3.up); 
} 

我相信你的「異常行爲」是由於使用歐拉角的,因爲他們並不總是完全可預測的。儘可能嘗試使用Quaternions或Vector3.Angle()。

如果你想限制角度(比如說...如果向下或向上傾斜超過45°則禁用該對象),您可以執行以下操作:

if (Vector3.Angle(forward, follow.transform.forward) > maxAngle) { ... }