Comments and answers for "GameObject Scale as World Coordinates (Units)?"
http://answers.unity.com/questions/475641/gameobject-scale-as-world-coordinates-units.html
The latest comments and answers for the question "GameObject Scale as World Coordinates (Units)?"Comment by Darkgaze on Darkgaze's answer
http://answers.unity.com/comments/1569518/view.html
[EDIT]
transform.bounds gives you the global size, axis oriented.
mesh.bounds gives you the local size of the mesh, which returns the same!. Instead of doing all that, which is a lot slower, depending on the size of the model.
------------------------------------------------------------------
Fixed C# Version.
Vertices are expressed locally to the object so this should work.
private Vector3 GetDimensions(GameObject obj)
{
Vector3 min = Vector3.one * Mathf.Infinity;
Vector3 max = Vector3.one * Mathf.NegativeInfinity;
Mesh mesh = obj.GetComponent<MeshFilter>().mesh;
for (int i = 0; i < mesh.vertices.Length; i++)
{
Vector3 vert = mesh.vertices[i];
min = Vector3.Min(min, vert);
max = Vector3.Max(max, vert);
}
// the size is max-min multiplied by the object scale:
return Vector3.Scale(max - min, obj.transform.localScale);
}Wed, 07 Nov 2018 10:18:18 GMTDarkgazeComment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1155142/view.html
Actually the lines should be
min = Vector3.Min(min, vert);
max = Vector3.Max(max, vert);
All those two lines does is to find the lowest and highest x / y / z value of all vertices.
So for example:
var min = Vector3.Min(new Vector3(10,2,-5), new Vector3(7,3,1));
var max = Vector3.Max(new Vector3(10,2,-5), new Vector3(7,3,1));
// min will be (7,2,-5) and max will be (10,3,1)
Since we store the result in min and max and use that variable again min will end up with the smallest values and max with the largest.
Actually there's no need for this method since mesh.bounds will return the same thing (just without the scaling at the end). Mesh.bounds is axis-aligned in local space while Renderer.bounds is axis-aligned in worldspace.
<img src="https://dl.dropboxusercontent.com/u/7761356/UnityAnswers/Images/Mesh.bounds.png">Tue, 15 Mar 2016 02:50:12 GMTBunny83Comment by MeTx on MeTx's answer
http://answers.unity.com/comments/1155111/view.html
I get a no overload error for the Vector3.Min/Max. apparently they take two arguments.
EDIT: put in vector3.zero as second argument for both. Seems to fine now. Thanks!
If you could instead explain why this works, I would be really grateful! :)Tue, 15 Mar 2016 00:16:03 GMTMeTxAnswer by aldonaletto
http://answers.unity.com/answers/475657/view.html
I suppose that you want to get the actual *dimensions* of the object in world units - am I right? If so, you could get the [collider][1], [renderer][2] or [mesh bounds][3]: the bounding box is an axis aligned box that fully encloses the object, and is expressed in world units. There's a problem, however: since it's axis aligned, a long object may appear bigger than it actually is if there's any rotation. If you want a more precise evaluation of actual dimensions, you must get the mesh vertices, multiply by the scale and find the max and min of each axis - something like this:
function GetDimensions(obj: GameObject): Vector3 {
var min: Vector3 = Vector3.one * Mathf.Infinity;
var max: Vector3 = Vector3.one * Mathf.NegativeInfinity;
var mesh: Mesh = obj.GetComponent(MeshFilter).mesh;
for (var vert: Vector3 in mesh.vertices){
min = Vector3.Min(vert);
max = Vector3.Max(vert);
}
// the size is max-min multiplied by the object scale:
return Vector3.Scale(max-min, obj.transform.localScale);
}
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Collider-bounds.html
[2]: http://docs.unity3d.com/Documentation/ScriptReference/Renderer-bounds.html
[3]: http://docs.unity3d.com/Documentation/ScriptReference/Mesh-bounds.html?from=RendererSun, 16 Jun 2013 18:40:51 GMTaldonaletto