【问题标题】:3D Text Rotation with the camera in Unity在 Unity 中使用相机进行 3D 文本旋转
【发布时间】:2017-09-21 17:38:01
【问题描述】:

我是 Unity3D 的初学者,与游戏开发有关。最近,我正在尝试做一个简单的程序,以便在 HoloLens 中实现它。目标是让 3D 文本(“_text”)沿相机移动的方向移动,效果非常好。但是,当我(使用 HoloLens)(+/-)90 度移动我的头时,我无法阅读文本,因为我没有面对文本,在 180 度时我看到我的 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");
        }

    }
}

【问题讨论】:

    标签: unity3d hololens


    【解决方案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);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 2022-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多