Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 aled96 · Mar 30, 2015 at 01:53 PM · rotatemapminimapradar

Minimap rotation

Hi, i have made (with the help of MakakWasTaken) a minimap, the camera is a child of the player and this is the code i attached to the camera:

public Transform player; public Texture enemyMarker; public Texture marker; List enemyPos = new List();

 void OnGUI() {
     Handles.DrawCamera (new Rect (5, 5, 105, 105),camera,  DrawCameraMode.Normal);
     foreach (Vector2 Pos in enemyPos) {
         if((Pos.x > 0 && Pos.x < 100) && (Pos.y > 0 && Pos.y < 100))
             GUI.DrawTexture(new Rect(5+Pos.x,5+Pos.y,enemyMarker.width,enemyMarker.height), enemyMarker);
     }
     
     GUI.DrawTexture(new Rect(55-(marker.width/2),55-(marker.height/2),marker.width,marker.height),marker);
 }

 void Update() {
     enemyPos.Clear(); //This might be .clear instead, I do not remember
     foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy")) {
         enemyPos.Add(Vector3ToMap(enemy.transform.position));
     }
 }

 Vector2 Vector3ToMap(Vector3 pos) {
     Vector2 ret;
     ret.x = pos.x-player.transform.position.x+50;
     ret.y = pos.z-player.transform.position.z+50;
     
     return ret;
 }

I have a problem, if my player turn and change position the camera (which i use to create the map turn with him) but how can I turn also the textures used to indicate enemy and player ? :)

Thank you so much :D

Comment
Add comment · Show 1
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 NathanHold · May 04, 2015 at 02:44 AM 0
Share

An easy way to achieve this is to put planes above the players and enemies with the textures that represent them and put them on a layer only the $$anonymous$$i-map will see. So it would be something like this:

  • Enemy -> Plane with enemy$$anonymous$$arker texture.

  • Player -> Plane with player$$anonymous$$arker texture.

This way you don't have to loop through the enemies and each enemy or player will automatically keep it all updated.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by IndieForger · May 04, 2015 at 06:00 AM

It is fairly simple fix if you are using GUI. You have to use GUIUtility.RotateAroundPivotto modify GUI.matrix (remember to restore it afterwards):

 GUIUtility.RotateAroundPivot(player.transform.localEulerAngles.y, playerBlipPosition);
        

Needed quick warm up before long day of work so here here is full example.

Just copy and paste that script, attach player and adjust zoomDelta parameter to fit your needs.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GUIMinimapController : MonoBehaviour {
 
     public Color minimapColor = new Color(1, 1, 1, 0.1f);
     public Rect minimapRect = new Rect(0, 0, 200, 200);
 
     public Color playerColor = new Color(0.5f, 1f, 0.5f, 1);
     public Rect playerMarker = new Rect(0, 0, 4, 4);
 
 
     public Color enemyColor = new Color(1, 0.5f, 0.5f, 1);
     public Rect enemyMarker = new Rect(0, 0, 2, 2);
 
     public Transform player;
 
     [Range(0 ,1)]
     public float zoomDelta = 0.4f;
     
     private Vector2 playerBlipPosition { get { return minimapRect.center; } }
 
     List<Vector2> enemyPositions = new List<Vector2>();
     GameObject[] enemies;
     Texture2D blipTexture;
 
     // Use this for initialization
     void Start () {
         blipTexture = new Texture2D(1,1);
         blipTexture.SetPixel(0, 0, new Color(1,1,1,1));
         blipTexture.Apply();
 
         enemies = GameObject.FindGameObjectsWithTag("Enemy");
     }
 
     void Update() {
         enemyPositions.Clear();
         Vector2 enemyPosition;
         foreach (GameObject enemy in enemies) {
             if (IsVisible(enemy.transform.position)) {
                 enemyPositions.Add(Vector3ToMap(enemy.transform.position));
             }
         }
     }
 
     bool IsVisible(Vector3 position) {
         float maxDist = ((minimapRect.width + minimapRect.height) / 4);
         return Vector3.Distance(position, player.transform.position) <= maxDist * zoomDelta;
     }
 
     void OnGUI() {
         Color color = GUI.color;
         Matrix4x4 matrix = GUI.matrix;
 
         // draw minimap
         GUI.color = minimapColor;
         GUI.DrawTexture(minimapRect, blipTexture);
 
         // draw enemies
         GUIUtility.RotateAroundPivot(player.transform.localEulerAngles.y, playerBlipPosition);
         GUI.color = enemyColor;
         foreach (Vector2 enemyPosition in enemyPositions) {
             Rect r = new Rect(enemyPosition.x, enemyPosition.y, enemyMarker.width, enemyMarker.height);
             GUI.DrawTexture(r, blipTexture);
         }
 
         // restore matrix
         GUI.matrix = matrix;
 
         // draw player
         GUI.color = playerColor;
         Rect playerRect = new Rect(playerBlipPosition.x - playerMarker.width / 2, 
                                    playerBlipPosition.y - playerMarker.height / 2, 
                                    playerMarker.width, 
                                    playerMarker.height);
         GUI.DrawTexture(playerRect, blipTexture);
 
 
                 // return color
         GUI.color = color;
     }
 
     Vector2 Vector3ToMap(Vector3 pos) {
         //Debug.Log (pos.x - player.transform.position.x);
         Vector2 position = new Vector2(
             playerBlipPosition.x + (pos.x - player.transform.position.x) / zoomDelta,
             playerBlipPosition.y + (pos.z - player.transform.position.z) / -zoomDelta
         );
         return position;
     }
 }


Few notes about the whole approach though:

  • GUI is not the best option for drawing minimaps, consider ugui to reduce draw calls

  • if you decide to use UGUI you can rotate your minimap as oppose to translating all the blips

  • try to avoid using GameObject.FindGameObjectsWithTag() in update method, it is expensive

  • you might want to consider enemy queue to loop through, enemy would register, add himself to the queue in Awake() and remove from it OnDestroy() then you can just loop through enemies in the queue

Happy coding.

Comment
Add comment · 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

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

20 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 avatar image avatar image

Related Questions

Creating an axes representation on the screen? 0 Answers

Minimap North Direction 1 Answer

Mini-Map skin? 1 Answer

Camera rotation around player while following. 6 Answers

Making a 2d "radar" for space game 2 Answers


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