Today I Learned :
구형 좌표 탐색
- Random.insideUnitSphere
- 3D 공간에서 원점(0,0,0)을 중심으로 반지름이 1인 구 안에 랜덤한 점 반환
- -1 부터 1 사이의 랜덤한 x, y, z 좌표를 생성
- Random.onUnitSphere
- 반지름이 1인 구 표면 상에 랜덤한 점을 반환
public void
{
Vector3 randomPosition = Random.onUnitSphere * spawnRadius + centerPos;
randomPosition.y = 100f;
GameObject animal = animals[Random.Range(0, animals.Length)].prefab;
Ray ray = new Ray(randomPosition, Vector3.down);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
randomPosition.y = hit.point.y;
//Debug.LogWarning("CreateAnimal " + randomPosition);
Instantiate(animal, randomPosition, Quaternion.identity);
}
else
{
Debug.LogWarning("Not Hit" + randomPosition);
}
}
- Physics.OverlapSphere
- 3D 공간에서 구 안에 있는 Collider들을 검출하는 데 사용
public void
{
int animalLayerMask = 1 << LayerMask.NameToLayer("Animal"); // 변경하려는 레이어 이름으로 대체하세요.
float checkRadius = 50.0f; // 검사하려는 범위 반지름
Collider[] hitColliders = Physics.OverlapSphere(transform.position, checkRadius, animalLayerMask);
int numberOfObjects = hitColliders.Length;
Debug.LogWarning("Number of objects in range: " + numberOfObjects);
if (numberOfObjects < 3)
{
// 실행
}
}
'TIL' 카테고리의 다른 글
20240219 TIL (0) | 2024.02.19 |
---|---|
20240215 TIL (0) | 2024.02.15 |
20240213 TIL (0) | 2024.02.13 |
20240207 TIL (0) | 2024.02.07 |
20240206 TIL (1) | 2024.02.06 |