Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Converting PolygonCollider2D.points from local to world space coordinates

Discussion in '2D' started by AssembledVS, Jul 16, 2015.

  1. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    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.

    Code (CSharp):
    1. void OnCollisionStay2D(Collision2D otherCollider)
    2. {
    3.     if (otherCollider.collider.GetType() == typeof(PolygonCollider2D))
    4.     {
    5.         otherPolygonCollider = otherCollider.collider.GetComponent<PolygonCollider2D>();
    6.  
    7.         for (int i = 0; i < otherPolygonCollider.points.Length; i++)
    8.         {
    9.             otherPolygonCollider.points[i] =
    10.             otherPolygonCollider.transform.TransformPoint(otherPolygonCollider.points[i]);
    11.  
    12.             if (playerCollider.bounds.Contains(otherPolygonCollider.points[i]) == true)
    13.             {
    14.                 print("contains vertex");
    15.             }
    16.         }
    17.     }
    18. }
     
    Last edited: Jul 18, 2015
  2. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    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:

    Code (CSharp):
    1. void OnCollisionStay2D(Collision2D otherCollider)
    2. {
    3.     if (otherCollider.collider.GetType() == typeof(PolygonCollider2D))
    4.     {
    5.         otherPolygonCollider = otherCollider.collider.GetComponent<PolygonCollider2D>();
    6.         for (int i = 0; i < otherPolygonCollider.points.Length; i++)
    7.         {
    8.             Vector2 worldSpacePoint = otherPolygonCollider.transform.TransformPoint(otherPolygonCollider.points[i]);
    9.             if (playerCollider.bounds.Contains(worldSpacePoint))
    10.             {
    11.                 print("contains vertex");
    12.             }
    13.         }
    14.     }
    15. }
     
    Edan-Smith likes this.
  3. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    Thanks! I can't test until tomorrow. I'll let you know if it works.
     
  4. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    The vertices are converting just fine now, thanks. The piece of code:

    if (playerCollider.bounds.Contains(worldSpacePoint)) { 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.