Last active
March 28, 2020 18:41
-
-
Save Sirataki/0ee53e6846aa47b104d3fce275787424 to your computer and use it in GitHub Desktop.
Get Collider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Sample : MonoBehaviour | |
{ | |
//判定の大きさ | |
public float radius; | |
//取得する対象となるレイヤー | |
public LayerMask targetLayer; | |
//サンプルとしてAnimator(Humanoidアバター)を用いて右手に判定を作る | |
protected Animator m_Animator; | |
protected Transform m_Avator_RightHand; | |
//取得するコライダーの領域を確保 | |
protected Collider[] m_OverlapResult = new Collider[16]; | |
void Start() { | |
m_Animator = GetComponent<Animator>(); | |
if (m_Animator.isHuman) { | |
m_Avator_RightHand = m_Animator.GetBoneTransform(HumanBodyBones.RightHand); | |
} | |
} | |
void Update() { | |
Vector3 worldPos = m_Avator_RightHand.transform.position; | |
//Physics.Overlap~NonAllocは取得しているコライダーの数を返す | |
int count = Physics.OverlapSphereNonAlloc(worldPos, radius, m_OverlapResult, targetLayer, QueryTriggerInteraction.Ignore); | |
for (int i = 0; i < count; ++i) { | |
//取得したコライダー | |
Debug.Log(m_OverlapResult[i].gameObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment