- Home /
Measuring the length of Objects
In Unity, how can you measure the length of an object? To make it more clear: What I mean is how long it is in the x axis. The objects have meshes etc.
I am also in need of an answer to this question.
I am working with a group of primitives that Instantiate themselves on a shelf.
Using the collider.bounds call I was able to write a script that caused the items to dynamically space themselves apart evenly as more objects appeared on the shelf.
However, I ran into a big problem when I tried to rotate that shelf about the Y axis to a position that was not aligned with world coordinate axis. It f'd up the whole thing!
I need a way to measure the primitives that does not use .bounds
I tried to use $$anonymous$$esh.bounds, as that provides coordinates in local space.
Help?
Answer by CHPedersen · May 23, 2011 at 08:48 AM
That totally depends on the object, and on what precisely you mean by length. If the object has a mesh, and the length you want is how long it stretches along one of the axes, you can get the bounding box of the mesh by either of,
GetComponent<MeshFilter>().mesh.bounds;
GetComponent<MeshRenderer>().bounds;
But what if the mesh is a crumbled up string of spaghetti in a relatively small bounding box, and you'd like to know how long that string of spaghetti is? Then the question is a lot harder to answer. Can you elaborate on what you mean by length?
Answer by mipo · Sep 13, 2013 at 02:04 PM
width = GetComponent(MeshFilter).mesh.bounds.extents.x;
$$anonymous$$y sliding door slides on a distance of width
and it is its full width - the door is open.
Answer by Deepscorn · Jan 03, 2016 at 07:14 PM
I agree to @CHPedersen, that in most cases that totally depends on the object, and on what precisely you mean by length. But the most straightforward and generic way is just get box, which encapsulates all the gameObject meshes inside. That's how this can be archived: (using tree iteration to correctly scale child elements with parent scale)
private static Bounds GetTotalMeshFilterBounds(Transform objectTransform)
{
var meshFilter = objectTransform.GetComponent<MeshFilter>();
var result = meshFilter != null ? meshFilter.mesh.bounds : new Bounds();
foreach (Transform transform in objectTransform)
{
var bounds = GetTotalMeshFilterBounds(transform);
result.Encapsulate(bounds.min);
result.Encapsulate(bounds.max);
}
var scaledMin = result.min;
scaledMin.Scale(objectTransform.localScale);
result.min = scaledMin;
var scaledMax = result.max;
scaledMax.Scale(objectTransform.localScale);
result.max = scaledMax;
return result;
}
Answer by Sakirma · Jun 24, 2015 at 11:03 AM
try to use
vector3 PositionOfTheHead = ((MeshFilter).mesh.bounds.extents.z * gameobject.transform.localScale.z) + gameobject.transform.position.z);
Answer by noknisieWin10 · Nov 23, 2017 at 09:56 AM
You can do that by using: yourobjectname.gameobject.transform.localScale.x
Your answer
Follow this Question
Related Questions
What does unity measure distance in? 3 Answers
How do I measure the distance between two points on a mesh? 0 Answers
Scale object to certain length 1 Answer
How to delete the last character of a string 3 Answers
C# Variables Transform vs GameObject 1 Answer