俩个要点:1 是判断物体在摄像机的前面 2 是判断物体的视口坐标在(0,0)至(1,1)之间
public bool IsInView(Vector3 worldPos) { Transform camTransform = Camera.main.transform; Vector2 viewPos = Camera.main.WorldToViewportPoint(worldPos); Debug.Log("viewPos.x:" + viewPos.x); Debug.Log("viewPos.y:" + viewPos.y); Vector3 dir = (worldPos - camTransform.position).normalized; float dot = Vector3.Dot(camTransform.forward, dir); //判断物体是否在相机前面 if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1) return true; else return false; } void Update() { if (IsInView(transform.position)) { Debug.Log("目前本物体在摄像机范围内"); } else { Debug.Log("目前本物体不在摄像机范围内"); } }