programmerdude619 - 8 months ago 61

C# Question

as the image below shows, I would like to find a random position within the blue area (B) and not the red area (A). How can I achieve this? A and B are currently 2 colliders. I just need a position within B but it cant be within A. Thanks in advance.

A and B areas

Answer

One Solution is same as Nico Schertler has mentioned in the comment

- Get the coordinate of each vertices of both the rectangle.
- take the x coordinate of min x of blue triangle and min coordinate of red triangle as a pair.(xminBlue,xminRed). Take the x coordinate of max x of red triangle and max coordinate of blue triangle as a pair.(xmaxRed,xmaxBlue)
- Do the same for Y coordinate and get (yminBlue,yminRed) . (ymaxRed,ymaxBlue)
- Use
`if(Random.value; < 0.5f){ x= Random.Range(xminBlue,xminRed); }else { x= Random.Range(xmaxRed,xmaxBlue); }`

- Do the same as 4 to get the value of y

In this solution u dont need to reject any coordinate

Source (Stackoverflow)