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?
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.
1 Like
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
public Transform pointA, pointB;
// You will place the objects into the inspector
// If this isn't possible, then you will need other means
// of obtaining through Find(), FindObjectsOfType(), etc..
Step 2:
Next you will need to determine the distance from your target object from pointA && pointB.
private Vector2 distFromA, distFromB;
// These will be used to store the distance calculations
// From target to pointA && target to pointB
// Inside of a function that is updating-able Update()
// That will keep track of movement updates
// Assuming your "target" is a GameObject and not a Transform
// Target could be "player' depends on what you're doing.
distFromA = pointA.position - target.transform.position;
distfromB = pointB.position - target.transform.position;
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:
private BoxCollider2D targetCollider;
// Depending on your collider type would require specific class
// so a CircleCollider you wouldn't declare as a BoxCollider
// This is just an example.
targetCollider.size = new Vector2(X,Y);
// You would want to plug your own X or Y
// coordinates into the Vector2.
// It's unclear to me whether or not you wanted the midpoint
// or whether you wanted something else so I left it blank.
1 Like