Can anyone enlighten me about bounds.contains() usage?


I want to perform a simple check to see if some vertices of a mesh are in a simple cubic area, so I try to use Bounds.Contains() function but my script doesn’t show anything :

#pragma strict
//This script is attached to a simple cube primitive
var meshToTest : Transform;
private var mesh : Mesh;
private var vertices : Vector3[];
private var howManyVerts : int;
private var pastpos : Vector3;

function Start () {
	//Get an array of all the vertices of a mesh
	mesh = meshToTest.GetComponent(MeshFilter).mesh;
	vertices = mesh.vertices;
	print (vertices.Length + " verts in mesh");//a simple check to be sure in founds the vertices
	print("coords: " +;
	pastpos =;

function Update () {
	if ( != pastpos){ //just to launch the detection if the cube is moved
		print("bounding box has moved");
		for (var i = 0; i < vertices.Length; i++){//looping through the array
			if (i == 0){
				print("vertex coords :" + vertices*);//just to ensure it goes in the loop (and it does at runtime)*
  •  	}*

_ if (renderer.bounds.Contains(vertices*)){//checking if the vertex position is inside cube’s bounding box*_
* howManyVerts++;*
* print (“found one!”);//this is never triggered at runtime :(*
* }*
* }*
* if (howManyVerts > 0){*
* print (howManyVerts + " vertices detected");//neither do this*
* howManyVerts=0;*
* }*

* pastpos =;*
* }*
Unfortunately Unity doc regarding this function is quite short, and I don’t put the finger on what I’m doing wrong here. Please help.

Bounds.Contains() works as you would expect. The problem with your code is that renderer.bounds in in world coordinates, but the vertices are in local coordinates. The easy solution is to just add a Transform.TransformPoint(). At line 27:

var testPoint = transform.TransformPoint(vertices*);*

if (renderer.bounds.Contains(testPoint) {
Or even combine it into a single line:
if (renderer.bounds.Contains(transform.TransformPoint(vertices*)) {*