Overlap 중 Sphere를 사용해 간단한 기능을 만들어보면...
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class HideBehavior : MonoBehaviour
{
public Collider[] CurrentCubes;
Collider[] cubesInsideZone;
Collider[] cubesOutsideZone;
private void FixedUpdate()
{
cubesInsideZone = Physics.OverlapSphere(this.transform.position, 10f);
EnableHide(cubesInsideZone, false);
cubesOutsideZone = CurrentCubes.Except(cubesInsideZone).ToArray();
EnableHide(cubesOutsideZone, true);
}
private void EnableHide(Collider[] cubes, bool enable)
{
foreach (var cube in cubes)
{
cube.GetComponent<MeshRenderer>().enabled = enable;
}
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(this.transform.position, 10f);
}
}
콜라이더를 적용한 오브젝트(HideBehavior)의 인스펙터 창에서 적용받을 큐브들을 드래그해 배열에 등록해줍니다.
결과:
이 배열에 등록되지 않은 오브젝트들은 OverlapSphere가 스칠 때 오브젝트가 제거되므로 주의해야 합니다.
이 곳을 참조했습니다.