I am attempting to convert PolygonCollider2D.points from local to world space coordinates, but I am not sure where I am going wrong in my logic.
This code is part of a larger script which is supposed to detect when the player character is touching a PolygonCollider2D vertex. The player’s Collider2D.bounds is in world coordinates, but PolygonCollider2D.points are in local coordinates. The issue is that the points do not seem to convert to world space, as when I print them back out to test them, they are the same coordinates in local space.
void OnCollisionStay2D(Collision2D otherCollider)
{
if (otherCollider.collider.GetType() == typeof(PolygonCollider2D))
{
otherPolygonCollider = otherCollider.collider.GetComponent<PolygonCollider2D>();
for (int i = 0; i < otherPolygonCollider.points.Length; i++)
{
otherPolygonCollider.points[i] =
otherPolygonCollider.transform.TransformPoint(otherPolygonCollider.points[i]);
if (playerCollider.bounds.Contains(otherPolygonCollider.points[i]) == true)
{
print("contains vertex");
}
}
}
}
I tested out your script and I think the problem is that you’re trying to assign the results of TransformPoint to the point on the collider. If you just use another variable to store the Vector2 it should work. Try this:
void OnCollisionStay2D(Collision2D otherCollider)
{
if (otherCollider.collider.GetType() == typeof(PolygonCollider2D))
{
otherPolygonCollider = otherCollider.collider.GetComponent<PolygonCollider2D>();
for (int i = 0; i < otherPolygonCollider.points.Length; i++)
{
Vector2 worldSpacePoint = otherPolygonCollider.transform.TransformPoint(otherPolygonCollider.points[i]);
if (playerCollider.bounds.Contains(worldSpacePoint))
{
print("contains vertex");
}
}
}
}
The vertices are converting just fine now, thanks. The piece of code:
if (playerCollider.bounds.[Contains](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Contains)(worldSpacePoint)) { [print](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=print)("contains vertex"); }
is not printing even when touching vertices, but I’m sure that I can figure this one out. I will write some code to mark the actual vertices to see if the vertices are where I think they are first.