• 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
Question by Cence99 · Jun 16, 2013 at 06:20 PM · gameobjecttransformscaleworldunits

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

Comment
aldonaletto

People who like this

1 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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

Comment
habsi70
MeTx
Bunny83

People who like this

3 Show 3 · 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 MeTx · Mar 15, 2016 at 12:16 AM 0
Share

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! :)

avatar image Bunny83 MeTx · Mar 15, 2016 at 02:50 AM 0
Share

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.

avatar image Darkgaze · Nov 07, 2018 at 10:18 AM 0
Share

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


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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

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

Related Questions

Can the insides of a gameObject affect its transform? 0 Answers

How can I attach a gameobject to another without changing its scale. 1 Answer

postion scale and rotation greyed out and not working 1 Answer

Calculating mesh over GameObject scale 1 Answer

How to reset GameObject to original scale 2 Answers


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