这个脚本最好是把模型对象的锚点设置在最低点。好了直接上脚本。可以直接复制代码,把CS文件拖到一个Camera上,然后把目标拖到targetTran中去就行了。
using UnityEngine; using System.Collections; public class CameraT3 : MonoBehaviour { // public public Transform targetTran; // private private Rect screenRect; private Camera camera; void Start () { screenRect = new Rect(0, 0, Screen.width, Screen.height); camera = GetComponent<Camera>(); } void Update () { if (screenRect.Contains(camera.WorldToScreenPoint(targetTran.position))) { Debug.Log("进入摄像机范围!"); } if (!screenRect.Contains(camera.WorldToScreenPoint(targetTran.position))) { Debug.Log("不在摄像机范围!"); } } }
时间: 2024-10-29 02:50:33