백지부터 시작하는 이세계 코딩 생활

Physics.OverlapSphere 본문

백지부터 시작하는 이세계 유니티 생활 since 2020

Physics.OverlapSphere

조아덕 2020. 12. 24. 18:02
Physics.OverlapSphere

정의해준 콜라이더(Collider) 범위 내에 접촉한 콜라이더에 대한 정보를 반환해준다.
특정 레이어(Layer)의 정보만을 반환 받을 수 있다. 이때, 배열의 형태로 값을 반환 받는다.

 


public static Collider[] OverlapSphere(Vector3 position, float radius, int layerMask = AllLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

 


//3D Example
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void ExplosionDamage(Vector3 center, float radius)
    {
        Collider[] hitColliders = Physics.OverlapSphere(center, radius);
        foreach (var hitCollider in hitColliders)
        {
            hitCollider.SendMessage("AddDamage");
        }
    }
}

 

//2D Example
public void Attack()
	{
		Vector3 pos = transform.position;
		pos += transform.right * attackOffset.x;
		pos += transform.up * attackOffset.y;

        Collider2D colInfo = Physics2D.OverlapCircle(pos, attackRange, attackMask);
        if (colInfo != null)
        {
            colInfo.GetComponent<PlayerHealth>().TakeDamage(attackDamage);
        }
    }

 

 


Ref.

1 docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

 

Unity - Scripting API: Physics.OverlapSphere

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

2 a-game-developer0724.tistory.com/54

 

[Unity 3D] Physics.OverlapSphere (주변 콜라이더 추출하기)

Physics.OverlapSphere - 중점과 반지름으로 가상의 원을 만들어 추출하려는 반경 이내에 들어와 있는 콜라이더들을 반환하는 함수 함수의 반환 값은 Collider 컴포넌트의 배열로 넘어옵니다. 또한 Overlap

a-game-developer0724.tistory.com

 

'백지부터 시작하는 이세계 유니티 생활 since 2020' 카테고리의 다른 글

Collider  (0) 2020.12.25
URP, Unity 2020.1.17  (0) 2020.12.25
MoveTowards  (0) 2020.12.24
Sorting Layer  (0) 2020.12.24
Image property in Inspector  (0) 2020.12.14
Comments