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
1
Question by Mun-Yeong-Seok · Sep 09, 2017 at 09:48 PM · 3dtriggerobjectheightstacking

How to measure the height of 3D objects.

3D objects with different shapes are stacked at some point. And I want to measure the maximum height of the point. Each 3D object is a thing, not a simple figure. As objects stacked, their heights also change. How should i measure the height? First, the way I think is: It is a way to use trigger. Move the trigger up and wait for OnTriggerExit() to be called. Is there a better way than this?

Comment
Add comment · Show 2
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 Cherno · Sep 09, 2017 at 10:46 PM 1
Share

You can use Renderer.bounds or Collider.Bounds

avatar image Mun-Yeong-Seok Cherno · Sep 09, 2017 at 11:40 PM 0
Share

Thanks! $$anonymous$$eshRenderer.bounds.max.y: Depending on the object, Y Position seems to have an error range of up to 0.9, but it seems to be usable. I will apply it.

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by IsaiahKelly · Sep 10, 2017 at 12:02 AM

Use a bounding box to calculate the height

The quickest and most accurate calculation is with Collider.bounds.size. Here is an example script that will grab the total bounding box size of all child objects under the object it is attached to. Then you simply grab the y value of the bounding box size to get the overall height.

 using UnityEngine;
 
 public class BoundsDebug : MonoBehaviour
 {
     // Visual display of the bounding box for testing.
     private void OnDrawGizmos()
     {
         // Draw each child's bounds as a green box.
         Gizmos.color = new Color(0f, 1f, 0f, 0.3f);
         foreach (var child in GetComponentsInChildren<Collider>())
         {
             Gizmos.DrawCube(child.bounds.center, child.bounds.size);
         }
 
         // Draw total bounds of all the children as a white box.
         Gizmos.color = new Color(1f, 1f, 1f, 0.1f);
         var maxBounds = GetMaxBounds(gameObject);
         Gizmos.DrawCube(maxBounds.center, maxBounds.size);
         Debug.Log("Total height is " + maxBounds.size.y);
     }
 
     Bounds GetMaxBounds(GameObject parent)
     {
         var total = new Bounds(parent.transform.position, Vector3.zero);
         foreach (var child in parent.GetComponentsInChildren<Collider>())
         {
             total.Encapsulate(child.bounds);
         }
         return total;
     }
 }

This script will produce the following results in the editor when you place game objects with colliders under a game object with this script attached to it. alt text It works quite well, but you'll notice that the upper left object here has gaps around it. This is because it appears that mesh colliders are automatically converted into box colliders for bounds calculations and this is what produces the gaps you see for non-square objects when rotated.

Use a raycast hit to calculate the height

Alternatively you could use a raycast to get more accurate readings for mesh colliders. However, the problem with this is you would need to figure out where to shoot the ray from to actual get the very top of the object. This would be no easy task, so it's probably a better idea to just check the actual height of the vertices on the meshes instead as suggested by fafase.


bounds-test.png (208.8 kB)
Comment
Add comment · Show 14 · 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 Mun-Yeong-Seok · Sep 10, 2017 at 12:19 AM 0
Share

$$anonymous$$ost objects have $$anonymous$$esh Collider applied, and the error range seems to same. It is exact if Collider is a figure.

avatar image IsaiahKelly Mun-Yeong-Seok · Sep 10, 2017 at 12:54 AM 0
Share

not sure what this error is you're talking about, but why are you using max ins$$anonymous$$d of size?

avatar image Mun-Yeong-Seok IsaiahKelly · Sep 10, 2017 at 01:24 AM 0
Share

For example, Find the vertex height that is the highest of the various vertices of a 3D object. If i rotate the object, the position of the vertex will change, and the maximum height will also change(Unless it is a balanced object). And Error Range.. When i rotate an object, there is a difference occurred between bounds.max.y and the actual object max height.

Show more comments
avatar image
2

Answer by zzeeshann · Sep 10, 2017 at 01:19 AM

If the object has a renderer,

 Vector3 sizeVec = GetComponent<Renderer>().bounds.size;

Next best thing would probably be collider

 Vector3 sizeVec = GetComponent<Collider>().bounds.size;
 
 Debug.Log(sizeVec.x);
 Debug.Log(sizeVec.y);
 Debug.Log(sizeVec.z);

You can also watch this video tutorial https://youtu.be/W-p0LFqJY_0

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
2

Answer by fafase · Sep 11, 2017 at 06:52 PM

If you need exact value, you can use the mesh and iterate through the vertices. If you consider the bottom object wont change because it is below it all, you can just store that guy for the rest.

  1. Get highest object

  2. Get lowest object ( you store this one as it may be the first item to be placed)

  3. Iterate vertices from 1 to find highest y value

  4. iterate vertices from 2 to find lowest y value (store that one if not likely to change)

  5. Do 3 - 4 to get the distance

Make sure that for steps 3 and 4 you convert their position to world space (vertex value is local space).

This will give exact distance regardless of the collider shape or the rotation of the objects.

As mentioned in the comment, you can use generation process to find the highest object on top. Iterate the collection of placed items, when one failed to be on top after 3 runs (or 4, or 2) remove it as it will never be on top. This will gradually set your collection to only the top items. Then iterate the y values only if you have a new topper.

Comment
Add comment · Show 5 · 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 IsaiahKelly · Sep 11, 2017 at 07:09 PM 0
Share

This is probably the most accurate, but also the most expensive process because there could easily be hundreds of vertices to check and you'd be repeating the action every time something changes or even every frame!

avatar image fafase IsaiahKelly · Sep 11, 2017 at 07:13 PM 0
Share

Indeed it is the most expensive. But dat accuracy tho...

On the other hand, you are only checking the vertices of the two far most objects, and only the y values. So the hit may not be so great. Considering your bottom object may not change, you got that value already from the start. So you are only checking the vertices of the top object. And you can also optimize the search for that one by discarding objects that have failed to be on top for like three searches. So you end up only checking like 4 or 5 items and only one set of y values of the top one.

avatar image Mun-Yeong-Seok fafase · Sep 11, 2017 at 07:57 PM 0
Share

yes. Here is my code.

     Vector3[] verts = meshFilter.mesh.vertices;
                     for (int i = 0; i < verts.Length; i++)
                     {
                         verts[i] = transform.TransformPoint(verts[i]);
                     }
                     float maxHeight = verts[0].y;
                     for (int i = 1; i < verts.Length; i++)
                     {
                         if (verts[i].y > maxHeight)
                         {
                             maxHeight = verts[i].y;
                         }
                     }

Whenever objects are added one by one, vertex calculation is performed on the object, and the maximum height value is updated by comparing with the previously calculated height value. Is it a bit different from what you told me? Something I have not written about is that objects are added sequentially.

Show more comments

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

92 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 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 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 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 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

How do I move an object from point A to point B using a trigger? 3 Answers

Is there a way to interpolate between meshes/shapes? I want to give an inflating (becoming a sphere) effect to an object. 0 Answers

Distance between a waypoint and the object 2 Answers

Trigger Icicles to drop 1 Answer

changing the 3D character or character's body after picking up an object 0 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