When a 3D object lies within the bounds of a 2D polygon(or a 3D polygon that extends infinitely on the Y-coordinate), a message should be triggered.
There are a number of 2D polygons that form a loop, with 2 co-ordinates of each polygon being shared with the previous polygon in the sequence.
I have tried using Rect() but am having trouble defining each rectangle with 4 co-ordinates available rather than width and height:
3D object to be detected = this Each 2D rectangle defined as (A = previousA, B = previousB, C = currentA, D = currentB)
void Update ()
{
Vector3 previousA = new Vector3(0, 0, 0);
Vector3 previousB = new Vector3(0, 0, 0);
Vector3 currentA;
Vector3 currentB;
Rect rect1;
foreach (GameObject item in CheckpointManager.GetComponent<Checkpoints>().checklines)
{
currentA = item.transform.GetChild(0).transform.position;
currentB = item.transform.GetChild(1).transform.position;
if (previousA == new Vector3(0, 0, 0) || previousB == new Vector3(0, 0, 0))
{
}
else
{
rect1 = new Rect(
previousA.x,
previousA.y,
Mathf.Sqrt( (previousA.x - currentB.x) * (previousA.x - currentB.x) ),
Mathf.Sqrt( (previousA.y - previousB.y) * (previousA.y - previousB.y))
);
if (rect1.Contains(this.transform.position))
{
Debug.Log("intersection confirmed in ploygon: " + rect1);
}
}
previousA = currentA;
previousB = currentB;
}
}
Edit 1:
The sample of code above is attactched to the 3D white cube object.
Here is a picture better demonstrating what I mean:
Here the camera is high on the Y-axis, with the Z-axis going up and down (north/south), and the X-axis is going left and right (east/west).
Each of the red and green icons mark a single point. Two adjacent red icons and two adjacent green icons mark the four points of a 2D polygon (connected by Unity gizmo lines). The final pair of points created will be connected to the first pair of points that were created. I have created a tool in the editor that can create pairs of points, so that there will always be a loop of pairs of points, and therefore always a loop of 2D polygons with four points.
The 3D White Cube needs to be aware of when it is currently contained within any of the 2D polygons, no matter how many there are on the screen, and no matter their rotation. It also needs to be aware of which polygon it is currently residing in, if any.
However, the 3D cube can be anywhere along the Y-axis (close to the camera or potentially infinitely far away).