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

Bounds.Contains

建议更改

成功!

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

关闭

提交失败

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

关闭

取消

声明

public bool Contains(Vector3 point);

描述

point 是否在包围盒内?

如果传递给 Contains 的点 point 在包围盒内,则返回 True 值。包围盒的最小值和最大值限制(角和边)上的点被视为在内部。

注意: 如果 Bounds.extents 在任何坐标中包含负值,则 Bounds.Contains 将始终返回 False。

//Attach this script to a GameObject with a Collider component
//Create an empty GameObject (Create>Create Empty) and attach it in the New Transform field in the Inspector of the first GameObject
//This script tells if a point  you specify (the position of the empty GameObject) is within the first GameObject’s Collider

using UnityEngine;

public class Example : MonoBehaviour { //Make sure to assign this in the Inspector window public Transform m_NewTransform; Collider m_Collider; Vector3 m_Point;

void Start() { //Fetch the Collider from the GameObject this script is attached to m_Collider = GetComponent<Collider>(); //Assign the point to be that of the Transform you assign in the Inspector window m_Point = m_NewTransform.position; }

void Update() { //If the first GameObject's Bounds contains the Transform's position, output a message in the console if (m_Collider.bounds.Contains(m_Point)) { Debug.Log("Bounds contain the point : " + m_Point); } } }