Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

3d Radar Map

Discussion in 'Scripting' started by Cyrussphere, Aug 31, 2016.

  1. Cyrussphere

    Cyrussphere

    Joined:
    Jul 1, 2016
    Posts:
    129
    Hi all,

    I am wanting to get a 3d Radar Minimap. By 3d I mean having the map include a line for above and below within a 3d space. I pulled the below code out of a unity book for a basic 2d map but can't seem to figure out how to get a 3d element into it or if it will even work with this. I am guessing that I need to draw some type of line render from the Blip Y position to the Y 0 position? but not sure how that might work on a UI.


    Code (CSharp):
    1.     //Mark for Astroid gameobjects in 3d world
    2.     private void DrawBlipsForAstroids(string tagName)
    3.     {
    4.  
    5.         // Find all game objects with tag
    6.         GameObject[] gos = GameObject.FindGameObjectsWithTag(tagName);
    7.  
    8.         // Iterate through them
    9.         foreach (GameObject go in gos)
    10.         {
    11.             drawBlip(go,astroidBlip);
    12.         }
    13.     }
    14.  
    15.     //Draw blip within the radar circle
    16.     void drawBlip(GameObject go, Texture aTexture)
    17.     {
    18.  
    19.         Vector3 centerPos=centerObject.position;
    20.         Vector3 extPos=go.transform.position;
    21.  
    22.         // first we need to get the distance of the enemy from the player
    23.         float dist=Vector3.Distance(centerPos,extPos);
    24.  
    25.         float dx=centerPos.x-extPos.x; //How far to the side of the player is the enemy?
    26.         float dz=centerPos.z-extPos.z; //how far in front or behind the player is to the enemy?
    27.  
    28.         // what's the angle to turn to face the enemy - compensating for the player's turning?
    29.         float deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;
    30.  
    31.         float bX = dist*Mathf.Cos(deltay * Mathf.Deg2Rad);
    32.         float bY = dist*Mathf.Sin(deltay * Mathf.Deg2Rad);
    33.  
    34.         bX=bX*mapScale;
    35.         bY=bY*mapScale;
    36.  
    37.         if (dist <= mapWidth * .5 / mapScale)
    38.         {
    39.             GUI.DrawTexture(new Rect(mapCenter.x + bX, mapCenter.y + bY, 4, 4), aTexture);
    40.         }
    41.  
    42.     }

    Oddly enough there didnt seem to be too much out there for 3d radars which surprises me. I found a free 3d Radar asset but it included more then i need and the code was just an ugly mess for things I really don't need. :)
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I was just sitting there in the sub-forum page, staring at this post title for like 5 minutes and wondering what this could possibly be referring to, not wanting to ruin the surprise by actually coming into the thread yet, and how I could make something like a 3D Radar Map if I needed to. I'm still not quite sure I understand what you want though- you just need a basic 2D map that has little blinking arrows pointing up or down from an object based on whether they're higher in the Y coordinate space than the player or lower?
     
  3. Cyrussphere

    Cyrussphere

    Joined:
    Jul 1, 2016
    Posts:
    129
    I have lack of better words to describe it other then 3d Radar system compared to the 2d flat radar system thats usually out there. I grabbed this off the free asset that was too much for what i need. But basically a radar with lines drawn up and down for the Y axis from their position on the radar.


     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    A few tips. First off, this asset is probably really old and really inefficient. It uses OnGUI to draw the radar, which IMO should only be used in editor scripts now as it's been replaced by the new UI/Canvas system. Updating this to work with the new system won't be trivial, although the math there is still fine.

    One big change that I'd make right away is to do away with the FindGameObjectsWithTag call and instead put a script on each object that needs to show up in this radar so that it reaches out and registers itself to the radar display manager (just calls a function that says "add me", and then gets added to a list- that list is what will get looped through for updating the blips on the radar). Everything that the object can calculate for itself, like its relative position to the player, it should do itself- the manager should only take those values from each object each frame and plot them graphically onto the radar, like an informational relay (which is what it is).

    As to your specific question, use a small graphic for the dot at the bottom (Y = 0), a graphic for the plane/object in the air (Y = normal), then a LineRenderer to draw between those two points. You'll need to use a Camera Overlay UI mode for the radar canvas, or else you won't be able to get the line to render on top of everything else. Alternatively, you can use a normal Overlay for the canvas type and just have a third little graphic for the "line" which you stretch out in the Y direction until it's the right height to fit between the dot and the plane graphic- this is pretty easy, and if it's just one solid color then the stretching isn't going to make it look bad or anything.

    Just some ideas!
     
  5. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    A simplistic solution of the top of my head would be to set up a mini scene on a separate radar-layer, with its own camera rendering to a texture (does not have to be super high resolution). I am guessing a separate set up is needed to keep distances small and avoid mipmaping and scaling effects.
    Exclude the radar-layer from the normal camera.
    If you want to reduce the radar update frequency (for performance reasons perhaps) you should be able to disable the camera and call the Camera.Render method at your own pace.

    Pro:
    - relatively easy to implement
    - no rolling your own projection math
    - object tracking can be done in the object being tracked, it just needs to update both its ingame and radar-object positions.
    - could use meshes and other effects instead of billboard
    - shaders, camera effects, filter assets etc can be used directly with the camera.​
    Con:
    - probably performance heavy compared to drawing pixels on a texture. (but it depends on what gains can be made in the code to find the objects to track and iterate through them, with findGameobjectsWithTag in there running through all the possible radar contacts each radar update cycle, it is not clear.)
    - endless possibilities encourages over-engineering.