Here is the function to draw a triangle that represents the camera field of view in a minimap. The problem with this code snippet is that when I rotate up and down (Change the pitch of camera) the viewport triangle also rotates up/down.
void makeViewPortCube()
{
if (player.GetComponent<Camera>() == null)
{
// Debug.LogError("Please assing a Main Camera to Follow");
return;
}
if (viewPortMesh == null)
{
viewPortGameobject = new GameObject();
viewPortGameobject.AddComponent<MeshFilter>().mesh = makeStartingMeshForViewPort(2.0f, 2.0f);
viewPortGameobject.AddComponent<MeshRenderer>().material = materiaForViewPortGameobject();
viewPortGameobject.name = "View Port Object";
viewPortMesh = viewPortGameobject.GetComponent<MeshFilter>().mesh;
viewPortGameobject.layer = LayerMask.NameToLayer("MiniMap");
viewPortGameobject.transform.position = new Vector3(viewPortGameobject.transform.position.x, /*transform.position.y-*/100, viewPortGameobject.transform.position.z);
}
// change vertices of the viewport
Vector3[] aVertexList = viewPortMesh.vertices;
if (stopRotation == false)
{
//Orignial
aVertexList[1] = player.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Screen.width, 0, 10f));
aVertexList[2] = player.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Screen.width, 0, player.GetComponent<Camera>().nearClipPlane));
aVertexList[3] = player.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(0, Screen.height / 4, player.GetComponent<Camera>().nearClipPlane));
aVertexList[0] = player.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(0, Screen.height / 4, 10f));
for (int i = 0; i < 4; i++)
{
aVertexList[i].y = 3;
}
}
viewPortMesh.vertices = aVertexList;
viewPortMesh.RecalculateBounds();
}