UI面向摄像机,直接用LookAt会发生偏转,导致UI倒置等问题。所以用了法线去算:
/*******************************************************************************
* 版本声明:v1.0.0
* 类 名 称:UILookAtCamera
* 创建日期:8/10/2019 5:29:59 PM
* 作者名称:末零
* 功能描述:UI面向摄像机
******************************************************************************/
using UnityEngine;
namespace LastZero
{
/// <summary>
/// UI面向相机
/// </summary>
public class UILookAtCamera : MonoBehaviour
{
public GameObject targetCamera;//目标相机
private Vector3 Normal = new Vector3(0, 1, 0);//面法线
private Quaternion direction;
private void Start()
{
direction = Quaternion.FromToRotation(new Vector3(0, 1, 0), Normal);
}
private void Update()
{
Look();
}
/// <summary>
/// 看向相机
/// </summary>
private void Look()
{
if (targetCamera == null)
return;
transform.rotation = targetCamera.transform.rotation * direction;
}
}
}
/*******************************************************************************
* 版本声明:v1.0.0
* 类 名 称:LookController
* 创建日期:8/13/2019 9:08:40 AM
* 作者名称:末零
* 功能描述:UI面向摄像机控制类
******************************************************************************/
using UnityEngine;
namespace LastZero
{
/// <summary>
/// UI面向摄像机控制类
/// </summary>
public static class LookController
{
/// <summary>
/// 添加UI面向摄像机脚本
/// </summary>
/// <param name="ui">UI</param>
/// <param name="camera">要面向的相机</param>
public static void AddLookAt(this GameObject ui, GameObject camera)
{
ui.AddComponent<UILookAtCamera>().targetCamera = camera;
}
}
}
返回目录