using UnityEngine; namespace Helpers { public static class InterferenceTransformer { /// /// Fits an object perfectly to the size of a container. The object wont surpass the container in any axis and will /// be the same size on at least one axis. /// /// The object that will be rescaled /// The container to which the object will be fitted public static void ScaleObject(GameObject objectToFit, GameObject container){ // compute the real scale of the object and container. the real scale is the scale in respect to the root game // object (instead of to the parent object) var realObjectScale = Vector3.Scale(objectToFit.transform.lossyScale, objectToFit.GetComponent().mesh.bounds.size); var realContainerScale = Vector3.Scale(container.transform.lossyScale, container.GetComponent().mesh.bounds.size); // compute the smallest ratio between object and container scale var ratio = Mathf.Min( realContainerScale.x / realObjectScale.x, realContainerScale.y / realObjectScale.y, realContainerScale.z / realObjectScale.z); // use the ratio to modify the local scale objectToFit.transform.localScale *= ratio; } public static void FloorObject(GameObject objectToFloor, GameObject container) { var delta = (objectToFloor.GetComponent().bounds.min - container.GetComponent().bounds.min); delta.x = 0; delta.z = 0; objectToFloor.transform.position -= delta; } } }