반응형
카메라 스크립트
using UnityEngine;
public class DisableWhenOutOfView : MonoBehaviour
{
private Camera mainCamera;
void Start()
{
mainCamera = Camera.main;
}
void Update()
{
Vector3 viewportPosition = mainCamera.WorldToViewportPoint(transform.position);
if (viewportPosition.x < 0 || viewportPosition.x > 1 || viewportPosition.y < 0 || viewportPosition.y > 1)
{
gameObject.SetActive(false);
}
}
}
오브젝트 코드
using UnityEngine;
public class DisableOutOfView : MonoBehaviour
{
private Renderer objectRenderer;
void Start()
{
objectRenderer = GetComponent<Renderer>();
}
void Update()
{
if (!objectRenderer.isVisible)
{
gameObject.SetActive(false);
}
}
}
반응형