Tz2/Assets/Zion/Scripts/LookTrans.cs

64 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 看向摄像机
/// </summary>
public class LookTrans : MonoBehaviour
{
public static LookTrans Instance;
/// <summary>
/// 主视角
/// </summary>
public Transform AimTrans;
/// <summary>
/// 需要看向摄像机的物体
/// </summary>
public Transform[] Trans;
/// <summary>
///
/// </summary>
Quaternion Qua;
/// <summary>
/// 旋转角度
/// </summary>
Vector3 RotV3 = Vector3.zero;
void Awake()
{
Instance = this;
}
void Update()
{
for (int i = 0; i < Trans.Length; i++)
{
if (Trans[i] && Trans[i].gameObject)
{
if (Trans[i].gameObject.activeSelf)
{
Trans[i].LookAt(AimTrans);
RotV3.y = Trans[i].rotation.eulerAngles.y;
Qua.eulerAngles = RotV3;
Trans[i].rotation = Qua;
}
}
}
}
/// <summary>
/// 添加需要看向摄像机的物体
/// </summary>
/// <param name="tran"></param>
public void TransAddTransform(Transform tran)
{
Transform[] _Trans=new Transform[Trans.Length+1];
for (int i = 0; i < Trans.Length; i++)
{
_Trans[i] = Trans[i];
}
_Trans[Trans.Length] = tran;
Trans = _Trans;
}
}