ShanDongVirtualPowerPlant/u3d-ShanDongVirtualPowerPlant/Assets/Adam/Scripts/Components/LookAt.cs

35 lines
772 B
C#

using UnityEngine;
public class LookAt : MonoBehaviour
{
private Camera _mainCam;
private bool revert;
public bool isLockY = false;
public bool isLockXZ = false;
// Start is called before the first frame update
void Start()
{
_mainCam = Camera.main;
revert = true;
}
// Update is called once per frame
void Update()
{
if (!_mainCam) return;
var dir = transform.position - _mainCam.transform.position;
var up = Vector3.up;
if (isLockY)
{
dir.y = 0;
}
else if (isLockXZ)
{
up.Set(dir.x, 0, dir.z);
dir = Vector3.ProjectOnPlane(dir, up);
}
transform.LookAt(transform.position + dir, up);
}
}