- Home /
How to find distance from center of object to the lowest point of object
I have FBX model and I want to find how high it should be (position) to its lowest face is on ground (high 0).
I tried renderer.bounds, collider.bounds, mesh.bounds but size of anything is 0,0,0.
This is how I create object:
GameObject obj = Instantiate(Resources.Load("I",typeof(GameObject))) as GameObject;
obj.name="kocka123";
Answer by fafase · Apr 15, 2014 at 12:25 PM
Get the mesh of your object and iterate through all of them to find which has the lowest y value.
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float lowest = Mathf.Infinity;
int i = 0;
while (i < vertices.Length) {
if(vertices[i].y < lowest) lowest = vertices[i].y;
i++;
}
Answer by kblood · Jun 06, 2020 at 02:45 AM
Looking at the meshfilter works, but you should also use transform.point to on the vertices to ensure you get their world-space position.
Using Linq you can do this: float minY = transform.GetComponentsInChildren().SelectMany(m => m.mesh.vertices).Min(v => transform.TransformPoint(v).y);
It iterates over all meshfilters in all children, selects all their vertices and finds the lowest/min y point.