版本: Unity 6 (6000.0)
语言英语
  • C#

Bounds.Intersects

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实阅读用户提出的每个建议的修改,并在适用的情况下进行更新。

关闭

提交失败

由于某些原因,您的建议修改无法提交。请 <a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public bool Intersects(Bounds bounds);

描述

另一个包围盒是否与该包围盒相交?

检查包围盒是否与另一个包围盒接触。如果 bounds 之间存在相交,则此方法将返回一个设置为 true 的布尔值。如果至少存在一个点同时包含在两个 bounds 中,则这两个 bounds 相交。

//Attach this script to an empty GameObject. Create 2 more GameObjects and attach a Collider component on each. Choose these as the "My Object" and "New Object" in the Inspector.
//This script allows you to move your main GameObject left to right. If it intersects with the other, it outputs the message to the Console.

using UnityEngine;

public class BoundsIntersectExample : MonoBehaviour { public GameObject m_MyObject, m_NewObject; Collider m_Collider, m_Collider2;

void Start() { //Check that the first GameObject exists in the Inspector and fetch the Collider if (m_MyObject != null) m_Collider = m_MyObject.GetComponent<Collider>();

//Check that the second GameObject exists in the Inspector and fetch the Collider if (m_NewObject != null) m_Collider2 = m_NewObject.GetComponent<Collider>(); }

void Update() { //If the first GameObject's Bounds enters the second GameObject's Bounds, output the message if (m_Collider.bounds.Intersects(m_Collider2.bounds)) { Debug.Log("Bounds intersecting"); } } }