- Home /
Draw trapezoid on minimap with support for any camera angle
Hi guys. I guess it's more of a math problem but I can't really figure it out on my own.
Say in Starcraft II your minimap box that represents your view area is a trapezoid. Now if you would tilt camera and change its angle so that you can see far away up to the skybox, trapezoid would gain a lot of height and it would be stretched on the minimap to cover almost all of it. This is done in for example Heroes of Newerth practice mode.
For me to build such trapezoid I guess I would need to get simply four points representing my view (translate bottom points and top points from screen to game world?). But I'm not sure if that's the way to do it. Any tips are welcome.
Answer by Cherno · Sep 30, 2013 at 03:02 PM
Ok, it's been a while since I wrote this and I just ripped the following portion from my larger minimap script, so it might not work in this form but it should be enough to get you the idea.
This is for drawing a basci rectangular box to represent your screen portion on the minimap, I tried to draw a accurate trapezoid representation by using some user-created GUI drawline script ut couldn't figure it out. For this reason, there are only two points need for the following example (top left and bottom right corner).
Basically, you cast rays at the corners of your main screen and store the hit points as WorldToViewportPoint coordinates of your minimap camera, then use those for drawing your box or lines or whatever in OnGUI.
// +++ MiniMap Stuff
var MiniMapLastPos : Vector2 = Vector2.zero;
var MiniMapAbsoluteWidth = 248.0; //in pixels
var MiniMapAbsoluteHeight = 124.0;
var MiniMapOffsetX : int = 20;
var MiniMapOffsetY : int = 64;
var ScreenCorner1 : Vector3;
var ScreenCorner2 : Vector3;
var MiniMapCorner1 : Vector3;
var MiniMapCorner2 : Vector3;
var MiniMapScreenOffsetX1 : int;
var MiniMapScreenOffsetY1 : int;
var MiniMapScreenOffsetX2 : int;
var MiniMapScreenOffsetY2 : int;
var MiniMapCamera : Camera;
MiniMapCamera = GameObject.Find("MiniMapCamera").camera;
var BorderTexture : Texture2d; //just a small transparent image with a white border
var MapBackgroundMask : LayerMask;
function Update()
{
//MiniMap Stuff
var MiniMapCurrentPos = Vector2(Screen.width, Screen.height);
if ((MiniMapLastPos - MiniMapCurrentPos).magnitude != 2)
{
MiniMapLastPos = MiniMapCurrentPos;
MiniMapCamera.pixelRect = new Rect(MiniMapOffsetX, Screen.height - MiniMapAbsoluteHeight - MiniMapOffsetY, MiniMapAbsoluteWidth, MiniMapAbsoluteHeight);
}
var ray1 : Ray = MainCamera.camera.ViewportPointToRay (Vector3(0,1,0));
var hit1 : RaycastHit;
if (Physics.Raycast (ray1, hit1, Mathf.Infinity, MapBackgroundMask))
{
ScreenCorner1 = Vector3(hit1.point.x, hit1.point.y, hit1.point.z);
//print (ScreenCorner1);
}
var ray2 : Ray = MainCamera.camera.ViewportPointToRay (Vector3(1,0,0));
var hit2 : RaycastHit;
if (Physics.Raycast (ray2, hit2, Mathf.Infinity, MapBackgroundMask))
{
ScreenCorner2 = Vector3(hit2.point.x, hit2.point.y, hit2.point.z);
//print (ScreenCorner2);
}
MiniMapCorner1 = MiniMapCamera.camera.WorldToViewportPoint(ScreenCorner1);
MiniMapCorner2 = MiniMapCamera.camera.WorldToViewportPoint(ScreenCorner2);
MiniMapScreenOffsetX1 = MiniMapAbsoluteWidth * MiniMapCorner1.x;
MiniMapScreenOffsetY1 = MiniMapAbsoluteHeight * MiniMapCorner1.y;
MiniMapScreenOffsetX2 = MiniMapAbsoluteWidth * MiniMapCorner2.x;
MiniMapScreenOffsetY2 = MiniMapAbsoluteHeight * MiniMapCorner2.y
}
function OnGUI()
{
var MiniMapWindow = new Rect(MiniMapScreenOffsetX1 + MiniMapWindowOffsetX, MiniMapWindowOffsetY - MiniMapScreenOffsetY1, MiniMapScreenOffsetX2 - MiniMapScreenOffsetX1, MiniMapScreenOffsetY1 - MiniMapScreenOffsetY2);
GUI.Box(MiniMapWindow,"MiniMap Box", BorderTexture);
}
I tried everything I could to get this script to work, but it keeps getting tripped up on the BorderTexture.
Answer by aanimation · Dec 23, 2014 at 05:20 AM
using UnityEngine;
using System.Collections;
public class sampleInCSharp : MonoBehaviour
{
public Vector2 MiniMapLastPos = Vector2.zero;
public float MiniMapAbsoluteWidth = 248.0f; //in pixels
public float MiniMapAbsoluteHeight = 124.0f;
public int MiniMapOffsetX = 20;
public int MiniMapOffsetY = 64;
public Vector3 ScreenCorner1;
public Vector3 ScreenCorner2;
public Vector3 MiniMapCorner1;
public Vector3 MiniMapCorner2;
public float MiniMapScreenOffsetX1;
public float MiniMapScreenOffsetY1;
public float MiniMapScreenOffsetX2;
public float MiniMapScreenOffsetY2;
Camera MiniMapCamera;
void Start ()
{
MiniMapCamera = GameObject.Find("CameraMiniMap").GetComponent<Camera>();
}
Texture2D BorderTexture; //just a small transparent image with a white border
LayerMask MapBackgroundMask;
void OnGUI()
{
//MiniMap Stuff
Vector2 MiniMapCurrentPos = new Vector2(Screen.width, Screen.height);
if ((MiniMapLastPos - MiniMapCurrentPos).magnitude != 2)
{
MiniMapLastPos = MiniMapCurrentPos;
MiniMapCamera.pixelRect = new Rect(MiniMapOffsetX, Screen.height - MiniMapAbsoluteHeight - MiniMapOffsetY, MiniMapAbsoluteWidth, MiniMapAbsoluteHeight);
}
Ray ray1 = Camera.main.ViewportPointToRay (new Vector3(0,1,0));
RaycastHit hit1;
if (Physics.Raycast (ray1, out hit1, Mathf.Infinity, MapBackgroundMask))
{
ScreenCorner1 = new Vector3(hit1.point.x, hit1.point.y, hit1.point.z);
Debug.Log (ScreenCorner1);
}
Ray ray2 = Camera.main.ViewportPointToRay (new Vector3(1,0,0));
RaycastHit hit2;
if (Physics.Raycast (ray2, out hit2, Mathf.Infinity, MapBackgroundMask))
{
ScreenCorner2 = new Vector3(hit2.point.x, hit2.point.y, hit2.point.z);
Debug.Log (ScreenCorner2);
}
MiniMapCorner1 = MiniMapCamera.camera.WorldToViewportPoint(ScreenCorner1);
MiniMapCorner2 = MiniMapCamera.camera.WorldToViewportPoint(ScreenCorner2);
MiniMapScreenOffsetX1 = MiniMapAbsoluteWidth * MiniMapCorner1.x;
MiniMapScreenOffsetY1 = MiniMapAbsoluteHeight * MiniMapCorner1.y;
MiniMapScreenOffsetX2 = MiniMapAbsoluteWidth * MiniMapCorner2.x;
MiniMapScreenOffsetY2 = MiniMapAbsoluteHeight * MiniMapCorner2.y;
Rect MiniMapWindow = new Rect(MiniMapScreenOffsetX1 + MiniMapScreenOffsetX1, MiniMapScreenOffsetY1 - MiniMapScreenOffsetY1, MiniMapScreenOffsetX2 - MiniMapScreenOffsetX1, MiniMapScreenOffsetY1 - MiniMapScreenOffsetY2);
GUI.Box(MiniMapWindow, BorderTexture);
}
}
its working, but i can see affects, i nned an rect as main Camera position in Minimap. Anyone can help me?
Please rephrase your question, I don't know what your problem is :)
i think this code be create an trapezoid to indicating main.camera position in $$anonymous$$imap camera view, right? i need to draw a rect as outline to indicate mainCamera position in $$anonymous$$imap too.
Technically, the code doesn't show a trapezoid to show the camera's position relative to the map on the $$anonymous$$imap, but rather to show the camera's field of view. A rectangle would only be possible in this regard if the main camera would look straight down onto the playing field.
So I have been messing with this for a good while now and cant get it working. Can you please give me a little more detail on where to place the script and the public variables? Also, what ultimately makes the GUI.Box a trapezoid? It seems like it will always just end up as a perfect rectangle because it just gets the width/height and position. Thank you!
First of all... It's been 4 years!
It doesn't matter which gameobject this script it attached to as long as it's in the scene and active.The $$anonymous$$ini$$anonymous$$apCamera variable needs to be assigned somehow, though either by code or by making it public and doing it manually via the inspector.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Issues with Sine Wave Generation and Rotation 0 Answers
Finding Distance between Angles and Points 2 Answers
Mathf.Lerp like User Define Function 2 Answers
Minimap rotating nodes 1 Answer