Search Unity

Is it possible to set the (x,y) coordinates of the four vertices of a BoxCollider2D?

Discussion in '2D' started by guzzo9000, Nov 15, 2019.

  1. guzzo9000

    guzzo9000

    Joined:
    Apr 17, 2019
    Posts:
    3
    I am making an RTS and I developed a highlight box that uses a lineRenderer to function. Because of this, I have 4 vertices available to me that I could easily implement into a box collider(which i need). I can't seem to find a way because you can only manipulate the offset and size. Is it simply not possible to use the vertices, or at least a top left location and length and width to resize the box collider?
     
  2. ShervinM

    ShervinM

    Joined:
    Sep 16, 2017
    Posts:
    67
    Calculating the size / offset of the box collider based on the vertices should be easy enough. Just get the distances between each two vertices that make up the width, height, and length to calculate the size, an then calculate their midpoints to find the center.
     
    MisterSkitz likes this.
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You are correct and to do this, how would you approach it? I think I would try:

    First step:
    Obtain the transforms of each point

    Code (CSharp):
    1. public Transform pointA, pointB;
    2.  
    3. // You will place the objects into the inspector
    4. // If this isn't possible, then you will need other means
    5. // of obtaining through Find(), FindObjectsOfType(), etc..
    Step 2:
    Next you will need to determine the distance from your target object from pointA && pointB.

    Code (CSharp):
    1. private Vector2 distFromA, distFromB;
    2.  
    3. // These will be used to store the distance calculations
    4. // From target to pointA && target to pointB
    5.  
    6. // Inside of a function that is updating-able Update()
    7. // That will keep track of movement updates
    8.  
    9. // Assuming your "target" is a GameObject and not a Transform
    10. // Target could be "player' depends on what you're doing.
    11. distFromA = pointA.position - target.transform.position;
    12. distfromB = pointB.position - target.transform.position;
    13.  
    14. Vector2 midPoint = new Vector2( (distFromA.x + distFromB.x) / 2.0f,  (distFromA.y + distFromB.y) / 2.0f  );
    Now you should be able to properly calculate the midpoint.

    Edit:

    I guess the real question here was how to adjust the collider size. To do this you'd:

    Code (CSharp):
    1. private BoxCollider2D targetCollider;
    2.  
    3. // Depending on your collider type would require specific class
    4. // so a CircleCollider you wouldn't declare as a BoxCollider
    5. // This is just an example.
    6.  
    7. targetCollider.size = new Vector2(X,Y);
    8. // You would want to plug your own X or Y
    9. // coordinates into the Vector2.
    10. // It's unclear to me whether or not you wanted the midpoint
    11. // or whether you wanted something else so I left it blank.
     
    Last edited: Nov 16, 2019
    vakabaka likes this.