Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Muzz5 · May 23, 2011 at 08:29 AM · lengthmeasure

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jasonbelmonti · Aug 19, 2011 at 03:48 PM -1
Share

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?

5 Replies

· Add your reply
  • Sort: 
avatar image
9

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by mipo · Sep 13, 2013 at 02:04 PM

width = GetComponent(MeshFilter).mesh.bounds.extents.x;

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Will Davis · Sep 13, 2013 at 02:14 PM 1
Share

bounds.extents.x is half the width is it not?

avatar image mipo · Sep 13, 2013 at 11:09 PM 0
Share

$$anonymous$$y sliding door slides on a distance of width and it is its full width - the door is open.

avatar image
2

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;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by noknisieWin10 · Nov 23, 2017 at 09:56 AM

You can do that by using: yourobjectname.gameobject.transform.localScale.x

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges