Skip to content

Instantly share code, notes, and snippets.

@Sirataki
Last active March 28, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sirataki/0ee53e6846aa47b104d3fce275787424 to your computer and use it in GitHub Desktop.
Save Sirataki/0ee53e6846aa47b104d3fce275787424 to your computer and use it in GitHub Desktop.
Get Collider
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