天天看点

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

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

继续阅读