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";
fafase
3
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_.y < lowest) lowest = vertices*.y;*_
i++;
}
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.