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. Dismiss Notice

How to set EdgeCollider2D position via script?

Discussion in 'Editor & General Support' started by h0nka, Jul 16, 2014.

  1. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    I can't find any proper documentation on this. I'm trying to set the vertex points of an edgecollider2D with a script. How does one go about a feat such as this?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    yfactory likes this.
  3. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Settting the 6. point to the cordiantes X: 2f Y:5.4f

    Code (CSharp):
    1. Vector2[] colliderpoints;
    2. colliderpoints = MyEdgeCollider2D.points
    3. colliderpoints[5] = new Vector2(2f,5.4f);
    4. MyEdgeCollider2D.points =  colliderpoints;
    5.  
    (Not tested)
     
    shaqeeb, chesetra27 and h0nka like this.
  5. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Thank you. This seems to work!
     
    Last edited: Jul 16, 2014
    shaqeeb likes this.
  6. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    You wouldn't happen to know how to set the edgecollider to be exactly at the top of a square sprite? I mean i can through trial and error kinda of get it close, but is there an exact way to place it on the top edge of the square?
     
    shaqeeb likes this.
  7. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Never mind. Figured it out by doing:

    Code (CSharp):
    1.  
    2.         edgeCol = gameObject.AddComponent<EdgeCollider2D> ();
    3.  
    4.         spriteSizeX = renderer.bounds.max.x - renderer.bounds.min.x;
    5.         spriteSizeY = renderer.bounds.max.y - renderer.bounds.min.y;
    6.  
    7.         Vector2[] colliderpoints;
    8.         colliderpoints = edgeCol.points;
    9.         colliderpoints[0] = new Vector2(-spriteSizeX, spriteSizeY);
    10.         colliderpoints [1] = new Vector2 (spriteSizeX, spriteSizeY);
    11.         edgeCol.points =  colliderpoints;
    12.  
     
  8. shaqeeb

    shaqeeb

    Joined:
    Jun 11, 2019
    Posts:
    1
    these lines of codes are working perfectly on my project... thank you for this...
     
  9. Arcadian-Unity

    Arcadian-Unity

    Joined:
    Mar 14, 2020
    Posts:
    4
    worked for me, thanks @fffMalzbier
    it looks like you can't modify a point directly which seems quite inefficient because
    you'll have to store all points in an array, modify array element/s and then assign that array to the edge collider points
    even if you wanna change only one point
     
  10. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Seems inefficient but the collider would be needed to rebuild anyway even if you only change one point.
    Unless you do need to modify massive colliders with thousands of points multiple times per frame you wont be running into performance issues.
    If you would need to modify it very frame you could keep the collider points array in your class and reuse them only modify what you like to have changed and reapply it, saves the reading it from the collider or allocationg new memeory very frame.