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

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);
}