unity3d:ugui世界坐标朝向摄像机

2023-08-24 14:18:00 浏览数 (1)

代码语言:javascript复制
using UnityEngine;
using System.Collections;

public class LookAtCam : MonoBehaviour {
    public Transform m_camTrans;
	// Use this for initialization
	void Start () {
	
	}
	
    void Update()
    {
        Vector3 relativePos = transform.position - m_camTrans.position;
        Quaternion lookAtRotation = Quaternion.LookRotation(relativePos, Vector3.up);
        this.transform.rotation = Quaternion.Lerp(this.transform.rotation, lookAtRotation,  Time.deltaTime);
    }
}

非插值

代码语言:javascript复制
public class BEBillboard3D : MonoBehaviour {

		public bool UsePosition = false;
		Camera cam;
			
		void Start() {
			cam = Camera.main;
		}

		void Update() {

			if(UsePosition) {
				Vector3 vDir = cam.transform.position - transform.position;
				vDir.Normalize();
				transform.rotation = Quaternion.LookRotation(-vDir);
			}
			else {
				transform.rotation = cam.transform.rotation;
			}
		}
	}

0 人点赞