# Check if the bounds of the Cube (Unity Primitive) is within EmptyGameObjectWithBoxCollider bounds

If the user has clicked within the EmptyGameObjectWithBoxCollider bounds a Cube must be added at that location BUT considering the EmptyGameObjectWithBoxCollider (let say of Scale (1, 0.5, 1)) bounds.

If the user clicks too far, let say to the left, of the EmptyGameObjectWithBoxCollider, the newly created Cube Size( let say of Scale (0.5,0.5,0.5)) may go out of the bounds of the EmptyGameObjectWithBoxCollider so the program must automatically rearrange it within the EmptyGameObjectWithBoxCollider bounds.

I am using Raycasting to know the exact location where the user has clicked and can even add a newly created Cube at that location but without Bound checks.

So How can I check the bounds of the Cube as well as EmptyGameObjectWithBoxCollider so that the cube is always placed within it.
In the below image the Highlighted in Green is EmptyGameObjectWithBoxCollider. The Cube added has no Bounds checks

With the bounds check the image should look like this

Assuming the rotation is 0,0,0, and that the collider is a box collider:

you can get the minimum position on x, y and z with something like this :

``````Vector3 worldCenter = transform.position + boxCollider.center;

Vector3 minimumXYZ = new Vector3(
worldCenter.x - boxCollider.size.x/2,
worldCenter.y - boxCollider.size.y/2,
worldCenter.z - boxCollider.size.z/2);
``````

and the maximum x,y and z with :

``````Vector3 worldCenter = transform.position + boxCollider.center;

Vector3 maximumXYZ = new Vector3(
worldCenter.x + boxCollider.size.x/2,
worldCenter.y + boxCollider.size.y/2,
worldCenter.z + boxCollider.size.z/2);
``````

after this, assuming the mesh is a cube of size 1, 1, 1(cube of unity), we can get the boundaries of the cube :

``````Vector3 cubeMinimumXYZ = new Vector3(
cube.transform.position.x - cube.transform.scale.x/2,
cube.transform.position.y - cube.transform.scale.y/2,
cube.transform.position.z - cube.transform.scale.z/2);
//and...
Vector3 cubeMaximumXYZ = new Vector3(
cube.transform.position.x + cube.transform.scale.x/2,
cube.transform.position.y + cube.transform.scale.y/2,
cube.transform.position.z + cube.transform.scale.z/2);
``````

then, we got all the data we need to check if the cube is in the boundaries :

``````//minimum :
if(cubeMinimumXYZ.x < minimumXYZ.x)
{
cube.transform.position += new Vector3(
minimumXYZ.x - cubeMinimumXYZ.x, 0, 0);
}
if(cubeMinimumXYZ.y < minimumXYZ.y)
{
cube.transform.position += new Vector3(0,
minimumXYZ.y - cubeMinimumXYZ.y, 0);
}
if(cubeMinimumXYZ.z < minimumXYZ.z)
{
cube.transform.position += new Vector3(0, 0,
minimumXYZ.z - cubeMinimumXYZ.z);
}

//and the maximum :

if(cubeMaximumXYZ.x > maximumXYZ.x)
{
cube.transform.position += new Vector3(
maximumXYZ.x - cubeMaximumXYZ.x, 0, 0);
}
if(cubeMaximumXYZ.y < maximumXYZ.y)
{
cube.transform.position += new Vector3(0,
maximumXYZ.y - cubeMaximumXYZ.y, 0);
}
if(cubeMaximumXYZ.z < maximumXYZ.z)
{
cube.transform.position += new Vector3(0, 0,
maximumXYZ.z - cubeMaximumXYZ.z);
}
``````

I think this should do it

good luck