I have a map that I can scroll (drag with mouse) and zoom.
Im trying to implement this function:
static void PinScrollZoomAtMapPos(Vector2 mapPos_p, ScrollRect scroll_p)
{
// this is to make a point on the map always in the same place on the
// screen, regardless of current zoom
Vector2 scrolledMapPos = new Vector2(
mapPos_p.x + scroll_p.content.localPosition.x,
mapPos_p.y + scroll_p.content.localPosition.y
);
Vector2 scrolledZoomedMapPos = new Vector2(
mapPos_p.x * scroll_p.content.localScale.x + scroll_p.content.localPosition.x,
mapPos_p.y * scroll_p.content.localScale.y + scroll_p.content.localPosition.y
);
Vector2 diff = scrolledMapPos - scrolledZoomedMapPos;
Debug.Log("diff: " + diff);
Vector2 offseted = new Vector2(
scroll_p.content.localPosition.x + diff.x,
scroll_p.content.localPosition.y + diff.y
);
scroll_p.content.localPosition = offseted;
}
(The scroll_p.content is the map.)
Although I think my logic makes sense at first, I see how it will never work, the 'diff' var should return 0 after subsequent calls to this function, but since I move the map pos, it will never happen. I dont know what I have to do..
Now doing this:
static void PinScrollZoomAtMapPos(Vector2 mapPos_p, ScrollRect scroll_p)
{
// this is to make a point on the map always in the same place on the
// screen, regardless of current zoom
//Vector2 scrolledMapPos = new Vector2(
// mapPos_p.x + scroll_p.content.localPosition.x,
// mapPos_p.y + scroll_p.content.localPosition.y
// );
Vector2 scrolledZoomedMapPos = new Vector2(
mapPos_p.x * scroll_p.content.localScale.x + scroll_p.content.localPosition.x,
mapPos_p.y * scroll_p.content.localScale.y + scroll_p.content.localPosition.y
);
Vector2 diff = mapPos_p - scrolledZoomedMapPos;
Debug.Log("diff: " + diff);
Vector2 offseted = new Vector2(
scroll_p.content.localPosition.x + diff.x,
scroll_p.content.localPosition.y + diff.y
);
scroll_p.content.localPosition = offseted;
}
Works, but the map becomes un-scrollable. I want to account only for zoom, like ignore the displace caused by the zoom (since it enlarges the map from the center).
Note this is different from centralizing the map at pos. The pin can be at any place, for example at 80% bottom right of the screen, say theres a city there, I want the city to keep at the exact same place on the screen, while I zoom in/out.
I feel is such a simple thing to do,yet Im struggling with this since yesterday.