Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by waxx · Sep 30, 2013 at 02:38 PM · mathminimap

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JasonC001 · Sep 25, 2014 at 06:47 PM 0
Share

I tried everything I could to get this script to work, but it keeps getting tripped up on the BorderTexture.

avatar image JasonC001 · Sep 25, 2014 at 06:47 PM 0
Share

Also, would you be willing to convert it to C#?

avatar image
0

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?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Dec 23, 2014 at 03:16 PM 0
Share

Please rephrase your question, I don't know what your problem is :)

avatar image aanimation · Dec 24, 2014 at 02:27 AM 0
Share

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.

avatar image Cherno · Dec 25, 2014 at 09:41 PM 0
Share

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.

avatar image Kirch21 Cherno · Nov 10, 2017 at 03:58 PM 0
Share

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!

avatar image Cherno Kirch21 · Nov 12, 2017 at 01:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges