Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Blyler · Feb 27, 2014 at 06:41 AM · vector3unity 2dcollider 2d

Get Width of 2d Game Object

I have 2d scene, and I have a game object with a PolygonalCollider2d attached to it. I just want to know how to programtically get the width of this object. Nothing on the polygonalcollider2d has a "bounds" or a "size" property. Even if it did have bounds or size, those return vector3 objects, and I don't understand how a vector3 could be used to calculate the width of a game object. From what I understand, a vector3 object gives the value of the distance from the origin of the scene (magnitude). How could the distance from the origin of the scene be used to determine the width of a game object?
I know that something about the way I'm thinking about this problem is wrong, but I don't know what that could be. Any help anyone could give me would be greatly appreciated.

Thanks!

Comment
Add comment · Show 3
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 robertbu · Feb 27, 2014 at 06:41 AM 0
Share

What kind of game object: Sprite or some other sort of mesh?

avatar image Blyler · Feb 27, 2014 at 08:33 AM 0
Share

Well, there's just the game object with no renderer, only a polygonalcollider2d. Can I not get the width of that? I'm not aware of any "type" it has, only that I selected "create -> empty game object". Also, the game object does have a sprite component as a child, that is roughly the same size as the polygonalcollider2d. However, I'd really like to get the width of the polygonalcollider2d. Also, if I do get the width, what unit of measure will it be in? I've been assu$$anonymous$$g pixels, but I'm starting to think I'm not dealing with pixels anymore, Toto... I want to use this width to assist in lining up clones of this object precisely next to each other. Hope that makes sense...

avatar image robertbu · Feb 27, 2014 at 08:58 AM 0
Share

I just wandered through the reference a bit for Collider2D and PolygonCollider2D. I did not find an simple way of getting the width. There is a way. A PolygonCollider2D has a list of points that define a closed path. You can cycle through this list and find $$anonymous$$ and max values. The difference between $$anonymous$$ and max will be the width. For alignment you may also need the pivot of the collider. That is, you may need the relationship of the $$anonymous$$imum or maximum point to the pivot of the game object. You can get this from the points.

If the collider is not rotated and the empty game object is not scaled, then you can do all of your calculations in the local coordinates of the points. If you are dealing with rotation, then you will want to do a Transform.TransformPoint() for each point before doing any calculation.

1 Reply

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

Answer by robertbu · Feb 27, 2014 at 06:15 PM

Your question would be better titled "Get the Width of a PolyonCollider2D." Here is a some code to do that calculation and a bit more. The SetMinMaxPoly2D() function finds the world position of the four vertices on the path that are the minimum and maximum of an axes aligned box. Given what I believe you are trying to do, these are the points you need (at least on the x axis). If you need to calculate how far your collider extends to the left and right from the object pivot point you can do:

 var leftExtent = transform.position.x - minX.x;  
 var rightExtent = maxX - transform.position.x

The width of the collider is only going to help you if your polygon collider is symmetrical left and right across the pivot point.

This code calculates the four points each frame. It takes into account rotation and positioning. It places four small spheres at the found vertices. If you click the left mouse button, it will display the width and height (what your questions asked for).

 #pragma strict
 
 private var minX : Vector3;
 private var minY : Vector3;
 private var maxX : Vector3;
 private var maxY : Vector3;
 
 var marker : Transform[] = new Transform[4];
 
 function Start() {
     for (var i = 0; i < marker.Length; i++) {
         marker[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
         marker[i].localScale = Vector3(0.1, 0.1, 0.1);
     } 
 }
 
 function Update() {
     SetMinMaxPoly2D();
     marker[0].position = minX;
     marker[1].position = minY;
     marker[2].position = maxX;
     marker[3].position = maxY;
     
     if (Input.GetMouseButtonDown(0)) {
         var width = maxX.x - minX.x;
         var height = maxY.y - minY.y;
         Debug.Log("Width = "+width);
         Debug.Log("Height = "+height);
     }
 }
 
 function SetMinMaxPoly2D() {
     minX = Vector3(Mathf.Infinity,0,0);
     minY = Vector3(0,Mathf.Infinity,0);
     maxX = Vector3(-Mathf.Infinity,0,0);
     maxY = Vector3(0,-Mathf.Infinity,0);
 
     var poly = GetComponent(PolygonCollider2D);
     for (var i = 0; i < poly.pathCount; i++) {
         var path = poly.GetPath(i);
         for (var v : Vector2 in path) {
             v = transform.TransformPoint(v);
             if (v.x < minX.x) 
                 minX = v;
             if (v.y < minY.y)
                 minY = v;
             if (v.x > maxX.x)
                 maxX = v;
             if (v.y > maxY.y)
                 maxY = v;
         }
     }
     
     var z = transform.position.z;
     minX.z = z;
     maxX.z = z;
     minY.z = z;
     maxY.z = z; 
 }

If your object is never rotated or scaled and if you are only interested in the X extents, you can cut this code down a fair bit.

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 Blyler · Feb 28, 2014 at 02:20 PM 0
Share

Wow! Thank you for going so far to answer this question! I'm surprised that something that seems so basic to me is so incredibly complex. I can't be the only person that needs to get width of 2d objects, right?

avatar image robertbu · Feb 28, 2014 at 04:36 PM 0
Share

There very well may be a simpler way to do this, but after some searches and reading the reference, I could not find one. The 2D stuff is new and I'm learning along with the rest of the community. If I spot a better way, I'll update the answer.

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

21 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

Related Questions

Score only adds 1 then stops counting!! 1 Answer

How to get correct scale for CircleCollider2D radius? 4 Answers

Joint2D Tutorials 1 Answer

Collider2D overlap with tiles in Tilemap and remove tiles 1 Answer

moveTowards not effected by maxDistanceDelta 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