boxCollider.bounds doesnt work on rotation

Hi, i am trying to collapse a ground with box collider.
I am getting vertices that inside of the box collider then collapse it. Trying to make trench
But if i rotate the trench it works weirdly.I found why. But i dont now how to prevent it9655058--1373948--2Uv6q.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class meshChanger : MonoBehaviour
     BoxCollider boxCollider;
    public GameObject meshh;
    List<GameObject> meshes = new List<GameObject>();
    private void Start()
        boxCollider =  GetComponent<BoxCollider>();

    private void Update()
        if(Input.GetKeyDown(KeyCode.K)) setMesh();

    private void OnTriggerEnter(Collider other)
        if (other.gameObject.layer != 8) return;
    private void OnTriggerExit(Collider other)
        if (other.gameObject.layer != 8) return;
    private void draw()
        BoxCollider boxCollider = GetComponent<BoxCollider>();

        Bounds localBounds = new Bounds(, boxCollider.size);

        const float radius = 0.1f;

        Gizmos.color =;

        Gizmos.DrawSphere(transform.TransformPoint(new Vector3(localBounds.min.x, localBounds.min.y, localBounds.min.z)), radius);
        Gizmos.DrawSphere(transform.TransformPoint(new Vector3(localBounds.min.x, localBounds.min.y, localBounds.max.z)), radius);
        Gizmos.DrawSphere(transform.TransformPoint(new Vector3(localBounds.max.x, localBounds.min.y, localBounds.max.z)), radius);
        Gizmos.DrawSphere(transform.TransformPoint(new Vector3(localBounds.max.x, localBounds.min.y, localBounds.min.z)), radius);
    public void setMesh()
        foreach(GameObject myMesh in meshes)
            MeshFilter meshFilter = myMesh.GetComponent<MeshFilter>();
            Mesh mesh = Instantiate(meshFilter.sharedMesh); // Create a copy of the mesh

            Vector3[] vertices = mesh.vertices;

            for (int i = 0; i < vertices.Length; i++)
                Vector3 worldVertex = meshFilter.transform.TransformPoint(vertices[i]);

                if (boxCollider.bounds.Contains(worldVertex))
                    // The vertex is inside the box collider
                    vertices[i] += Vector3.down * 20f;

            mesh.vertices = vertices;
            //meshh.GetComponent<MeshCollider>().sharedMesh = mesh;

            meshFilter.mesh = mesh;


may be I could rotate the points of the collider.

You don’t. This is how AABB bounds work. They don’t get rotated.

You can probably easily check if a vertex is within the collider by checking if it’s position is close enough to it’s center, within it’s size. But at that point, you don’t need a collider, it’s just math.

Also your code is going to leak a ton of meshes in memory.

When you rotate the collider, it rotates it perfectly. As you discovered, Bounds is an AABB. Why are you using Bounds? What are you trying to accomplish here? Why are you dynamically generating a mesh based on the bounds? Why don’t you just have a cube mesh that you are rotating?

What’s the goal here??

(In case you aren't familiar with the term, Axially Aligned Bounding Box. They're always aligned with the world space axes. Colliders especially need this information because they occupy cells in an internal octree structure that is also permanently aligned with the world space axes, but rendering/culling queries also use them.)

1 Like

I am familiar with i told i found why. I am not asking about Axially Aligned Bounding Box. I

that is why there is boxCollider.bounds.Contains(worldVertex). It is doing exactly what you are saying.

My main problem is collapsing the vertices that inside of the collider. I will deal unused meshes later.

and i can destroy it with just this Destroy(meshFilter.mesh);

I have no idea what are you saying. I am not generating mesh based on bounds. I am collapsing vertices that inside of the bound. What do you mean by Why don’t you just have a cube mesh that you are rotating?. I am rotating it ?

See this is important context you didn’t share. That’s why I’m asking what you are trying to do. It sounds like you are trying to test if a given point is within the collider or not? To do that you can use this function:

// Check if the world space point is inside the collider or not.
bool IsWorldPointInCollider(Collider c, Vector3 worldPoint) {
  Vector3 closestPoint = c.ClosestPoint(worldPoint);

  // if it's the same point, then the input point was inside the collider
  return closestPoint == worldPoint;

This function relies on the behavior of ClosestPoint in which it returns the same position you give it when the position is inside the collider.


Thanks a lot its working. Actually i wrote it on the post