旋转物体始终面向摄像机, Scene和Game 场景中都可以使用,
直接使用LookAt(...)方法的话会有问题,物体面向摄像机的同时会绕Z轴旋转, 解决方法就是改为, 不是面对摄像机而是面对摄像机所在的平面.
/// 始终面向摄像机
/// </summary>
public class SimpleBillboard : MonoBehaviour
{
public bool IsStopInSceneView = false;
void OnDrawGizmos()
{
if (IsStopInSceneView == false && Application.isPlaying == false && SceneView.currentDrawingSceneView.camera != null)
{
Rot(SceneView.currentDrawingSceneView.camera.
transform);
}
}
void Update()
{
Rot(Camera.main.camera.transform);
}
void Rot(Transform target)
{
Plane plane = new Plane(target.forward, target.position);
float dis;
Vector3 tar = target.position;
if (plane.Raycast(new Ray(this.transform.position, -target.forward),out dis) == true)
{
tar = this.transform.position + (-target.forward * dis);
}
this.transform.LookAt(tar, Vector3.up);
}
}