我需要知道相机何时停止看到某个对象以将其返回到池中。 我尝试了一些方法,但没有成功,我想指出的是,相机仅在 X 轴上跟随玩家,它是 3D,但它是一款横向卷轴 View 游戏。 顺便说一句,我没有使用 MonoBehaviour,所以避免建议 OnBecameInvisible 和亲戚。
这是我的两次失败的尝试。
internal static bool IsVisible(this Renderer renderer)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
internal static void IsVisible(this GameObject go)
{
var screenPoint = Camera.main.WorldToScreenPoint(go.transform.position);
screenPoint.x = Mathf.Clamp01(screenPoint.x);
if (screenPoint.x > .05f)
{
Debug.Log("Visible");
}
else Debug.Log("Invisible");
}
最佳答案
简单:
if(!GetComponent<Renderer>().isVisible){
//Whatever you want to do here
}
如果您遇到问题,请参阅:
https://docs.unity3d.com/ScriptReference/Renderer-isVisible.html
https://forum.unity.com/threads/how-do-i-use-renderer-isvisible.377388/
关于unity-game-engine - 检查对象是否在屏幕外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505849/