center | ボックスの中心 |
halfExtents | 各軸についてのボックスサイズの半分 |
orientation | ボックスの回転 |
layerMask | レイヤーマスク はレイキャストするときに選択的に衝突を無視するために使用します。 |
queryTriggerInteraction | トリガーに設定されているものも検索対象にするか |
Collider[] 指定したボックスと重なっているコライダー
指定したボックスと重なっているコライダーをすべて見つけます。
Creates an invisible box you define that tests collisions by outputting any colliders that come into contact with the box.
//Attach this script to your GameObject. This GameObject doesn’t need to have a Collider component //Set the Layer Mask field in the Inspector to the layer you would like to see collisions in (set to Everything if you are unsure). //Create a second Gameobject for testing collisions. Make sure your GameObject has a Collider component (if it doesn’t, click on the Add Component button in the GameObject’s Inspector, and go to Physics>Box Collider). //Place it so it is overlapping your other GameObject. //Press Play to see the console output the name of your second GameObject
//This script uses the OverlapBox that creates an invisible Box Collider that detects multiple collisions with other colliders. The OverlapBox in this case is the same size and position as the GameObject you attach it to (acting as a replacement for the BoxCollider component).
using UnityEngine;
public class OverlapBoxExample : MonoBehaviour { bool m_Started; public LayerMask m_LayerMask;
void Start() { //Use this to ensure that the Gizmos are being drawn when in Play Mode. m_Started = true; }
void FixedUpdate() { MyCollisions(); }
void MyCollisions() { //Use the OverlapBox to detect if there are any other colliders within this box area. //Use the GameObject's centre, half the size (as a radius) and rotation. This creates an invisible box around your GameObject. Collider[] hitColliders = Physics.OverlapBox(gameObject.transform.position, transform.localScale / 2, Quaternion.identity, m_LayerMask); int i = 0; //Check when there is a new collider coming into contact with the box while (i < hitColliders.Length) { //Output all of the collider names Debug.Log("Hit : " + hitColliders[i].name + i); //Increase the number of Colliders in the array i++; } }
//Draw the Box Overlap as a gizmo to show where it currently is testing. Click the Gizmos button to see this void OnDrawGizmos() { Gizmos.color = Color.red; //Check that it is being run in Play Mode, so it doesn't try to draw this in Editor mode if (m_Started) //Draw a cube where the OverlapBox is (positioned where your GameObject is as well as a size) Gizmos.DrawWireCube(transform.position, transform.localScale); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.