1、直接上代码,挂载到物体上即可
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 限制旋转,只让跟着摄像机y轴旋转
/// </summary>
public class LookAtCam : MonoBehaviour
{
private Transform m_camTrans;
void Start()
{
m_camTrans = Camera.main.transform;
}
void Update()
{
Vector3 v = m_camTrans.position;
v.y = transform.position.y;
this.transform.LookAt(v);
}
}
2、结果