2017-09-21 92 views
1

我是Unity3D的初學者,並且都與遊戲開發相關。最近,我試圖做一個簡單的程序,以在HoloLens中實現它。我們的目標是讓3D文字(「_text」)在相機移動的方向上移動,這非常有效。然而,當我將我的頭部(與HoloLens相同)(+/-)90度移動時,我無法閱讀文本,因爲我沒有面對文本,180度時我看到了我的3d文本翻轉。如果有人能幫助我,我將不勝感激。 :)在Unity中使用相機進行3D文本旋轉

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class TextManager : MonoBehaviour { 

    public GameObject _text; 
    public TextMesh _startText; 

    // Use this for initialization 
    void Start() { 
     if (_text == null) _text = GameObject.Find("StartText"); 
     if (_startText == null) _startText = GameObject.Find("StartText").GetComponent<TextMesh>(); 
    } 

    // Update is called once per frame 
    void Update() { 

     if (_text.activeSelf) 
     { 
      var camPos = Camera.main.transform.position + Camera.main.transform.forward; 
      _text.transform.position = camPos; 
      _text.transform.localScale = Vector3.one * 0.025f;    
     } 

     else 
     { 
      Debug.Log("deactive _startText"); 
     } 

    } 
} 

回答

1

爲了得到一個廣告牌的行爲(文一直在尋找相機),你必須應用更改相機旋轉文本網以及:

_text.transform.rotation = Camera.main.transform.rotation; 

爲了獲得更新興的3D體驗有時可能對於在相機後退時將文本翻轉180°很有幫助,但會留下整體方向。要做到這一點:

 Vector3 objectNormal = _text.rotation * Vector3.forward; 
     Vector3 cameraToText = _text.transform.position - Camera.main.transform.position; 
     float f = Vector3.Dot (objectNormal, cameraToText); 
     if (f < 0f) 
     { 
      _text.Rotate (0f, 180f, 0f); 
     }