So I’m looking to click on my map to add “markers” on it - like in Hollow Knight’s map system.
My map system is based on a 2nd camera that renders in Orthographic mode and outputs this to a Render Texture, which I then display on a Raw Image in the UI system. My game camera displays in perspective mode.
So I found some answers to help based on this old post
and I’ve implemented that, shown below:
//local coordinates of my render texture's rect
RectTransformUtility.ScreenPointToLocalPointInRectangle(_screenRectTransform, eventData.position, null, out Vector2 localClick);
//my render texture's Y axis is reversed (yMax is 0 and yMin is negative), so I un-reverse this effect on my local click coordinates
localClick.y = (_screenRectTransform.rect.yMin * -1) - (localClick.y * -1);
// normalize my local rect coordinates to 0-1 viewport coordinates (correcting for the reverse Y axis again)
Vector2 viewportClick = new Vector2(localClick.x / _screenRectTransform.rect.xMax, localClick.y / (_screenRectTransform.rect.yMin *-1));
//use the second camera to convert the viewport coordinates to world coordinates
Vector2 worldClick = mapCamera.ViewportToWorldPoint(viewportClick);
//instantiate object at worldClick position
GameObject clickPoint = Instantiate(pinkPixel);
clickPoint.transform.position = worldClick;
It kinda seems to work a bit, but I want to be able to zoom in and out in the map, and the orthographic size of the mapCamera creates a huge offset from where I click to where the marker shows up. The more I zoom out (increase orthographic size) - the greater the offset.
I’ve attached an image to showcase what I mean. I traced the room in the map, which should be displaying right on the position where I clicked, but they have an offset and seem to have a scaling issue.
Any ideas?