Unity—UI正對攝像機

using UnityEngine;
using System.Collections;

public class CameraFacing : MonoBehaviour {   //掛在需要看向攝像機的UI物體上(例如血條,傷害冒字)

    private Camera refCamera;
    public bool reverFace = false;
    private Transform mRoot;

    private void Awake()
    {
        if (!refCamera)
        {
            refCamera = Camera.main;
        }
        mRoot = transform;
    }

    private void Update()
    {
        Vector3 targetPos = mRoot.position + refCamera.transform.rotation * (reverFace?Vector3.back:Vector3.forward);
        Vector3 targetOrientation = refCamera.transform.rotation * Vector3.up;
        mRoot.LookAt(targetPos, targetOrientation);
    }
}

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