Adjust the size of the object within a parent container
Hello,
I am using a library to load 3d models and because they are extremely large when imported into the scene I am trying to fit their size inside a container with scale (1, 1, 1).
I am using bouinds to be able to determine the size of the container and based on this to adjust the size of the imported model.
I attach an image of the result I am looking for
Basically what I am doing after importing the model or object is assigning it inside the parent container, calculating the parent bounds and based on the latter adjusting the size of the object to fit inside the container. After importing the model and applying the rescaling code it continues to go beyond the limits of the parent container
This is my code:
public void ModelReescaler(GameObject mesh) { if (mesh != null) { mesh.transform.localScale = new Vector3(1, 1, 1); _parentSize = mesh.transform.parent.GetComponent<BoxCollider>().bounds.size; while (mesh.GetComponentInChildren<Renderer>().bounds.max.x > _parentSize.x || mesh.GetComponentInChildren<Renderer>().bounds.max.y > _parentSize.y || mesh.GetComponentInChildren<Renderer>().bounds.max.z > _parentSize.z) { mesh.transform.localScale *= _valueScaler; } } }
Could someone tell me what I am doing wrong or if there is a better way to do it?
Your answer
Follow this Question
Related Questions
Adding additional C# scripts after build (Android, iOS) 1 Answer
Changing an objects location 1 Answer
[uNet] Multiple Spawnpoints with Selection 0 Answers
Mathf.Abs always returns 0 0 Answers
[SOLVED]Need OnTriggerEnter Help 2 Answers