1. 程式人生 > >Unity3D開發小貼士(十二)Billboard

Unity3D開發小貼士(十二)Billboard

遊戲中常常需要讓某個物件始終朝向攝像機,在世界UI上使用的比較廣泛,例如對話方塊或者血條。

常見的實現方法有兩種。

一種是直接改變rotation:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class BillboardRotation : MonoBehaviour {

	public Camera cameraLook;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (cameraLook == null) {
			cameraLook = Camera.main;
		}
		transform.rotation = cameraLook.transform.rotation;
	}
}

第二種是使用LookAt方法:
using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class BillboardPosition : MonoBehaviour {
	public Camera cameraLook;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (cameraLook == null) {
			cameraLook = Camera.main;
		}
		transform.LookAt (cameraLook.transform.position);
	}
}