JiNanCementPlantForUnity/Assets/GameAssets/H补充/Scripts/LookAt.cs

36 lines
857 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAt : MonoBehaviour
{
private Camera mainCamera;
public bool b;
public Vector3 r = new Vector3(-180, 0, 180);
void Start()
{
// 获取场景中的主摄像机
mainCamera = Camera.main;
}
void Update()
{
if (b)
{
// 获取物体到摄像机的方向向量
Vector3 direction = mainCamera.transform.position - transform.position;
// 让物体朝向摄像机的方向
transform.rotation = Quaternion.LookRotation(direction + r);
}
else
{
transform.LookAt(mainCamera.transform);
var rot = transform.localEulerAngles;
rot += r;
transform.localEulerAngles = rot;
}
}
}