I can not solve the problem. When creating a game object from the prefab, a triangle is drawn by the calculated points (it is drawn exactly and correctly) and I assign the same points to the vertices of PolygonCollider2D. According to my logic, the collider should coincide with the drawn triangle, but it leads to the side ...

pref.GetComponent<PolygonCollider2D>().points = new[]{point1, point2, point3}; pref.GetComponent<PolygonCollider2D>().SetPath(0, new[]{point1, point2, point3}); 

    1 answer 1

    Understood. The coordinates of the vertices are measured from the center of the object, that is,

     Vector2 p2 = new Vector2(point2.x - point1.x, point2.y - point1.y); Vector2 p3 = new Vector2(point3.x - point1.x, point3.y - point1.y); pref.GetComponent<PolygonCollider2D>().points = new[]{new Vector2(0, 0), p2, p3}; pref.GetComponent<PolygonCollider2D>().SetPath(0, new[]{new Vector2(0, 0), p2, p3}); 
    • Not from the center of the object, but relative to the pivot point. In the case of a newly created polygon, the pivot is in the center of the polygon. - Andrew
    • And here, thanks for this clarification! - IncDD