Search Unity

Map And blip system

Discussion in 'Scripting' started by Manawydan, Nov 25, 2015.

  1. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    So i am try make one map and blip system with GUI, so if the blip position is one center of map, the blip(image) position is center of image. I try make but failed.

    the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class MapBehaviour : MonoBehaviour {
    6.     public Texture mapTexture; // map texture
    7.     [SerializeField]private float mapLimitX,mapLimitY,mapScreenX,mapScreenY; // map real size x and y, map screen size x and y(iam use resolution 800:600)
    8.     public bool isShowing = false; // render this?
    9.     public BlipBehaviour[] blips; // blip array
    10.  
    11.     void OnGUI(){
    12.         if(isShowing){
    13.             RenderMap(); // render map function
    14.         }
    15.     }
    16.  
    17.  
    18.     void RenderMap(){
    19.         // draw map in center screen, resize to all resolutions
    20.         GUI.DrawTexture(new Rect(Screen.width/2-GetRelativePosition(mapScreenX/2,true),Screen.height/2-GetRelativePosition(mapScreenY/2,false),GetRelativePosition(mapScreenX,true),GetRelativePosition(mapScreenY,false)),mapTexture);
    21.  
    22.         // get all blips array
    23.         for(int i=0;i<blips.Length;i++){
    24.             // calcule x blip position
    25.             float _x = GetBlipScreenPosition(blips.transform.position.x,mapLimitX,Screen.width,blips.sizeX,mapScreenX);
    26.             // calcule y blip position
    27.             float _y = GetBlipScreenPosition(blips.transform.position.y,mapLimitY,Screen.height,blips.sizeY,mapScreenY);
    28.  
    29.             _x = GetRelativePosition(_x,true); // resize x blip position to all resolution
    30.             _y = GetRelativePosition(_y,false); // resize y blip position to all resolution
    31.  
    32.             Rect rectangle = new Rect( // rect
    33.                 (_x),
    34.                 (_y),
    35.                 GetRelativePosition(blips.sizeX,true), // resize blip size x
    36.                 GetRelativePosition(blips.sizeY,false)); // resize blip size y
    37.  
    38.             GUI.DrawTexture(rectangle,blips.icon); // draw
    39.         }
    40.     }
    41.  
    42.     float GetBlipScreenPosition(float blipP, float mpRealSize,float screen, float blipSize, float mpScreen){ // util functions
    43.         float r = (blipP/mpRealSize)*screen*(mpScreen/screen);
    44.         return r;
    45.     }
    46.  
    47.     float GetRelativePosition(float a,bool isWidth){ // util functions 2
    48.         float b = 0;
    49.         float c = 0;
    50.         if(isWidth){
    51.             b = Screen.width;
    52.             c = 800f;
    53.         }
    54.         else{
    55.             b = Screen.height;
    56.             c = 600f;
    57.         }
    58.         return(a/c)*b;
    59.     }
    60. }
     

    Attached Files:

    • map.jpg
      map.jpg
      File size:
      32.9 KB
      Views:
      706
    Last edited: Nov 25, 2015
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I think you left out the BlipBehaviour script.
     
  3. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    Ok, sorry for this
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BlipBehaviour : MonoBehaviour {
    5.     public float sizeX,sizeY;
    6.     public Texture icon;
    7.     public string iconName;
    8.  
    9.     void Start(){
    10.         icon = Resources.Load<Texture>(iconName);
    11.     }
    12. }
    13.  
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    A lot of your problems come from accessing the variable blips (which is an array of BlipBehaviours) as though it were a single blip, i.e., dereferencing public member variables, accessing its .transform property, etc.

    Instead I made a variable called blip that references the single blip we are working on in the for loop. That must be what you are intending to do, I think.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [ExecuteInEditMode]
    6. public class MapBehaviour : MonoBehaviour {
    7.     public Texture mapTexture; // map texture
    8.     [SerializeField]private float mapLimitX,mapLimitY,mapScreenX,mapScreenY; // map real size x and y, map screen size x and y(iam use resolution 800:600)
    9.     public bool isShowing = false; // render this?
    10.     public BlipBehaviour[] blips; // blip array
    11.  
    12.     void OnGUI(){
    13.         if(isShowing){
    14.             RenderMap(); // render map function
    15.         }
    16.     }
    17.  
    18.  
    19.     void RenderMap(){
    20.         // draw map in center screen, resize to all resolutions
    21.         GUI.DrawTexture(new Rect(Screen.width/2-GetRelativePosition(mapScreenX/2,true),Screen.height/2-GetRelativePosition(mapScreenY/2,false),GetRelativePosition(mapScreenX,true),GetRelativePosition(mapScreenY,false)),mapTexture);
    22.      
    23.         // get all blips array
    24.         for(int i=0;i<blips.Length;i++){
    25.             // get a reference to ONE blip out of the blips array
    26.             BlipBehaviour blip = blips[i];
    27.             // calcule x blip position
    28.             float _x = GetBlipScreenPosition(blip.transform.position.x,mapLimitX,Screen.width,blip.sizeX,mapScreenX);
    29.             // calcule y blip position
    30.             float _y = GetBlipScreenPosition(blip.transform.position.y,mapLimitY,Screen.height,blip.sizeY,mapScreenY);
    31.          
    32.             _x = GetRelativePosition(_x,true); // resize x blip position to all resolution
    33.             _y = GetRelativePosition(_y,false); // resize y blip position to all resolution
    34.          
    35.             Rect rectangle = new Rect( // rect
    36.                                      (_x),
    37.                                      (_y),
    38.                                      GetRelativePosition(blip.sizeX,true), // resize blip size x
    39.                                      GetRelativePosition(blip.sizeY,false)); // resize blip size y
    40.          
    41.             GUI.DrawTexture(rectangle,blip.icon); // draw
    42.         }
    43.     }
    44.  
    45.     float GetBlipScreenPosition(float blipP, float mpRealSize,float screen, float blipSize, float mpScreen){ // util functions
    46.         float r = (blipP/mpRealSize)*screen*(mpScreen/screen);
    47.         return r;
    48.     }
    49.  
    50.     float GetRelativePosition(float a,bool isWidth){ // util functions 2
    51.         float b = 0;
    52.         float c = 0;
    53.         if(isWidth){
    54.             b = Screen.width;
    55.             c = 800f;
    56.         }
    57.         else{
    58.             b = Screen.height;
    59.             c = 600f;
    60.         }
    61.         return(a/c)*b;
    62.     }
    63. }
    64.  
     
  5. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    So thanks, but my problem is with math, let's say Map Limit x and y is 60, Map Screen x is 400, Map Limit y is 300,
    and the blip position is (30,30), so the blip position in screen should be (400,300) center of screen. But this not happen, the blip screen position is (200,300), i try multiply by 2, but no work with all possible value from blip position(only work if blip position in map is center). Look in the Red Square(the blip)

    Wrong:


    Right:
     
  6. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    so i guess my math is wrong, how fix it?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You're doing a linear scaling mapping of a 2-dimensional vector, so without looking at your code, here are the steps you must do to get each axis from one coordinate system to the other:

    - first subtract out the notional "zero" (could be the center, could be lower left, upper left, etc.) of the world where the game object that you are showing the position of is, in your world

    - multiply by the size of the full deflection of the target coordinate system (in this case your minimap width and height in OnGUI coordinate space)

    - divide by the size of the full deflection of your world (where we started out above) in world space

    - possibly invert the Y value's sign (OnGUI Y coordinates get larger going down, whereas world Y coordinates get larger going up)

    - finally add in the zero of the minimap, whatever you choose that to be such that it correlates with the in-game zeropoint.

    These are the steps and they must be done in this particular order for each X and Y axis. Check your code over, and once you are doing the steps above, your coordinate scaling should work fine.

    Obviously, you should put in test code to put one map element at the zero, make sure it shows up at the zero. Then move it over 50% and 50% up and make sure it works. Step by step you will solve it with the guide above.
     
  8. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    Thanks man work fine!:)