- Home /
GameObject Scale as World Coordinates (Units)?
Good evening,
I've got a quiet annoying problem..
I need to get the scale of a GameObject, unlike transform.localScale which gives me (1, 1, 1) as the default scale of my model, I need to get the scale as coordinates (or the coordinate system's units).
I hope you understand what I mean.
It sounds very simple but I can't find any solution.
Thank you for each answer!
Mike
Answer by aldonaletto · Jun 16, 2013 at 06:40 PM
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, renderer or mesh bounds: 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);
}
I get a no overload error for the Vector3.$$anonymous$$in/$$anonymous$$ax. apparently they take two arguments. EDIT: put in vector3.zero as second argument for both. Seems to fine now. Thanks!
If you could ins$$anonymous$$d explain why this works, I would be really grateful! :)
Actually the lines should be
$$anonymous$$ = Vector3.$$anonymous$$in($$anonymous$$, vert);
max = Vector3.$$anonymous$$ax(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 $$anonymous$$ = Vector3.$$anonymous$$in(new Vector3(10,2,-5), new Vector3(7,3,1));
var max = Vector3.$$anonymous$$ax(new Vector3(10,2,-5), new Vector3(7,3,1));
// $$anonymous$$ will be (7,2,-5) and max will be (10,3,1)
Since we store the result in $$anonymous$$ and max and use that variable again $$anonymous$$ 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). $$anonymous$$esh.bounds is axis-aligned in local space while Renderer.bounds is axis-aligned in worldspace.
[EDIT]
transform.bounds gives you the global size, axis oriented. mesh.bounds gives you the local size of the mesh, which returns the same!. Ins$$anonymous$$d 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 $$anonymous$$ = Vector3.one * $$anonymous$$athf.Infinity;
Vector3 max = Vector3.one * $$anonymous$$athf.NegativeInfinity;
$$anonymous$$esh mesh = obj.GetComponent<$$anonymous$$eshFilter>().mesh;
for (int i = 0; i < mesh.vertices.Length; i++)
{
Vector3 vert = mesh.vertices[i];
$$anonymous$$ = Vector3.$$anonymous$$in($$anonymous$$, vert);
max = Vector3.$$anonymous$$ax(max, vert);
}
// the size is max-$$anonymous$$ multiplied by the object scale:
return Vector3.Scale(max - $$anonymous$$, obj.transform.localScale);
}