Minimap clicking RTS style feature

Hello,

Can someone please help me solve this. In my RTS game, which is a side-scroller (camera view is the same as 2D platformers) I need to implement the feature where, when I click on the minimap, the main camera view moves/teleports to that relative position.

How can I accomplish this ?

Thank you

PS: I’m using a camera to draw the minimap.

Assuming your are rendering your minimap with a camera that is looking into your scene, I think the easiest way to accomplish this is to simply have a collider in the scene that represents your minimap camera’s view in the scene.

Then, when you click within your minimap rendered view, you can do a raycast into the scene and get the corresponding hit location on your scene collider, then just use that location directly to set your main camera. Of course you’d probably want to put this collider in a special layer so that you only raycast hit that collider specifically.

And your collider could be on a gameobject that is a child of the minimap camera, so as you move in your side-scroller, the collider in the scene moves along with your cameras. Easy peasy.

You could map the pixels on the map’s texture to world coordinates. For example, assuming the map is 128x32 pixels and the world is 100x10 meters then clicking on pixel 55x15 would transport the camera to position (x:(100/128)*55, y:? z:(10/32)*15).

You just need to figure out which pixel the user clicked and the size of the world and map.

This code is totally not working for me. My worldpoint should be between 0-160, and 0-90, and after raycast on the minimap I get stupid values like -1 and 10…
it even modifies when I change the resolution of the screen. It’s working on main camera, but not on minimap camera, it hit’s the background, but the position totally sucks.