- Home /
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!
What kind of game object: Sprite or some other sort of mesh?
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...
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.
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.
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?
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.