Unity 如何使對象(文本(Text)\模型\精靈等)正面始終面向屏幕


using UnityEngine;
using System.Collections;
 
public class CameraFacingBillboard : MonoBehaviour
{
    public Camera m_Camera;
 
    //Orient the camera after all movement is completed this frame to avoid jittering
    void LateUpdate()
    {
        transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
            m_Camera.transform.rotation * Vector3.up);
    }
}

請注意,腳本不僅將對象指向相機。取而代之的是,它使對象指向與照相機前向軸相同的方向(即照相機向內看的方向)。從直覺上看,這可能是錯誤的,但實際上對於實時計算機圖形的單點觀點是正確的。

 

 

//    CameraFacing.cs 
//    original by Neil Carter (NCarter)
//    modified by Hayden Scott-Baron (Dock) - http://starfruitgames.com
//  allows specified orientation axis
 
 
using UnityEngine;
using System.Collections;
 
public class CameraFacing : MonoBehaviour
{
    Camera referenceCamera;
 
    public enum Axis {up, down, left, right, forward, back};
    public bool reverseFace = false; 
    public Axis axis = Axis.up; 
 
    // return a direction based upon chosen axis
    public Vector3 GetAxis (Axis refAxis)
    {
        switch (refAxis)
        {
            case Axis.down:
                return Vector3.down; 
            case Axis.forward:
                return Vector3.forward; 
            case Axis.back:
                return Vector3.back; 
            case Axis.left:
                return Vector3.left; 
            case Axis.right:
                return Vector3.right; 
        }
 
        // default is Vector3.up
        return Vector3.up;         
    }
 
    void  Awake ()
    {
        // if no camera referenced, grab the main camera
        if (!referenceCamera)
            referenceCamera = Camera.main; 
    }
        //Orient the camera after all movement is completed this frame to avoid jittering
    void LateUpdate ()
    {
        // rotates the object relative to the camera
        Vector3 targetPos = transform.position + referenceCamera.transform.rotation * (reverseFace ? Vector3.forward : Vector3.back) ;
        Vector3 targetOrientation = referenceCamera.transform.rotation * GetAxis(axis);
        transform.LookAt (targetPos, targetOrientation);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM