Search Unity

3D minimap

Discussion in 'Scripting' started by id0, May 31, 2019.

  1. id0

    id0

    Joined:
    Nov 23, 2012
    Posts:
    455
    OK, I'm bad with math. Let's say I have small 3d model of level in scale say 100: 1. I need to track the player on this minimap. A specific marker indicates a player, and he must be on this small model, where the player is on the real scene. How can i do this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    It's basically ratios: first you subtract out any world offset from the player's position, then you multiply by the difference in scale, then you add in any offset for the target minimap.

    Let's say the zero point on the minimap is at (1,2,3)

    Let's say that point corresponds to this point in your real world: (50,50,50)

    Let's say your minimap is 100 times smaller.

    Then the computation is:

    Code (csharp):
    1. Vector3 PlayerPosition = transform.position;
    2.  
    3. PlayerPosition -= new Vector3( 50, 50, 50); // remove world center
    4.  
    5. PlayerPosition /= 100;  // scale it down
    6.  
    7. PlayerPosition += new Vector3 (1,2,3);  // add back in minimap center
    8.  
    9. // now the PlayerPosition variable contains where you want to put the minimap icon for the player.
    10.  
    Start simple in an empty scene to try and nail down what you're doing with this, and get a better understanding of the underlying scaling transform, and then you can move it more confidently into your actual game.

    For all the above "magic numbers" you would traditionally substitute either an invisible marker GameObject, or else some variables, so you can tweak and adjust it appropriately.
     
    id0 and SparrowGS like this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You could simply use transform.position / 100f + minimap.position, but I'd like to suggest something a little more easily customizable.

    First, place an empty object in your scene at the exact point in real space that your minimap's origin corresponds to. e.g. if your origin is at the tip of mountain X in the minimap model, place this reference object in the real world at the top of the same mountain. Now, set the empty object's scale to (100,100,100). Have your minimap script hold a reference to this object, let's call it mapReferencePoint.

    Now, when your minimap script places an object representing the player's marker, you can use this to set it at the correct position:
    Code (csharp):
    1. Vector3 normalizedPos = mapReferencePoint.InverseTransformPoint( realWorldObject.transform.position);
    2. mapMarker.position = minimap.transform.TransformPoint(normalizedPos);
    This will let you easily modify the centerpoint of your map however you need to.
     
    id0 and Kurt-Dekker like this.
  4. id0

    id0

    Joined:
    Nov 23, 2012
    Posts:
    455
    Thanks, I'll try it.