Unity 中判断某一个游戏对象是否在摄像机范围内

在游戏制作中常常遇到过一个对象如果不在摄像机范围内的时候就会对该对象进行回收到节点池中去,所以就会在实际项目代码中判断游戏对象是否在摄像机范围内:

1:首先在onStart中初始化screenRect用来检测某个游戏对象是否还在摄像机范围内:

this.screenRect = new Rect(0,0,Screen.width,Screen.height);
        camera = this.palyerNode.transform.Find("Main Camera").GetComponent<Camera>();

2:在Update中检测一个游戏对象:

if(screenRect.Contains(camera.WorldToScreenPoint(gameObject.transform.GetChild(0).position)))
        {
            Debug.Log("第一个cube在视野内");
        } else
        {
            Debug.Log("第一个cube不在视野内");
        }

猜你喜欢

转载自blog.csdn.net/lck8989/article/details/104294800