2017-09-21 80 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); 
     }