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); } }