Unity 物體始終正對攝像機

using UnityEngine;

public class Text3DController : MonoBehaviour
{
    private GameObject _mainCamera;

    // Start is called before the first frame update
    private void Start()
    {
        _mainCamera = GameObject.FindWithTag("MainCamera");
    }

    // Update is called once per frame
    private void Update()
    {
        //物體始終面向攝像機
        var rotation = Quaternion.LookRotation(_mainCamera.transform.TransformVector(Vector3.forward),
            _mainCamera.transform.TransformVector(Vector3.up));
        rotation = new Quaternion(0, rotation.y, 0, rotation.w);
        gameObject.transform.rotation = rotation;
    }
}

腳本掛在需要面向攝像機的物體上即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章