Search Unity

Offsetting map coordinates for canvas

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Sep 15, 2018.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I'm working on a really crude minimap system using Unity's UI. Right now I spawn one icon on my minimap canvas for every friendly/enemy unit on the map, and set the icon's RectTransform.position to its gameobject.transform.position. As a first step, this works fine- I get tons of icons that correctly represent their position and relationship to each other on the map.

    However, this isn't all that useful on its own- I'd like to draw the player's icon at my minimap's RectTransform.rect.center, then apply the same offset to every other icon so they display with the correct relative positioning. This feels like a really absurdly simple thing to be stuck on, but given my rect.center and the coordinates of our desired center point in world space, how do I offset every other point proportionally?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    Set all icons' positions to
    (unit.transform.position - player.transform.position) * map_scale
    . Note that (player.transform.position - player.transform.position) will always be Vector3.zero, making it centered.

    It will get a little more complicated if you want to rotate the map so that "up" matches player.transform.forward. Hint:
    player.transform.InverseTransformPoint(unit.transform.position)
    .
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    That is tremendously helpful, thank you! :)

    This is getting down to tinkering, but here's my full implementation per your advice:

    Code (csharp):
    1.  
    2.             GameObject relativeObject; //Whatever the map should be centered on; the player character, or anything else. All other icons show relative to this guy!
    3.             Vector3 mapCenter = canvasRect.position; //Center of the sensor UI, as determined by its rect's position
    4.             Vector3 mapOffset = mapCenter - relativeObject.transform.position; //The difference between the map's center and the relative object is the amount to offset everything by
    5.             Vector3 desiredPosition = ((sensorContact.myObject.transform.position - relativeObject.transform.position) + mapOffset) * mapScale; //Position each icon relative to relativeObject, add the offset, multiply in the scale
    6.  
    7.             sensorContact.myRectTransform.position = desiredPosition;
    8.  
    It works, and the map's display is accurate to the positioning of objects in the game world, but moving relativeObject (which represents the player or whatever other object exists at the map's center). I assume I'm doing something funny with how I'm calculating and applying mapOffset, but it looks okay to me: it's recalculated every time the minimap updates, and since it's calculated relative to our reference object and the UI's center, it shouldn't be able to migrate out of the middle of the map like it does.
     
    Last edited: Sep 15, 2018