- Home /
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.
Still looking for ideas. Thanks PS: I don't find it normal to downvote my bump. It's not that I said/did something bad.
Answer by whebert · Aug 18, 2013 at 03:38 PM
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.
Hey,
Thank you for the great and simple idea. I managed to make it work..somewhat. It is not there yet, but close. Wherever I click, the main camera moves at approximately the same location.
This is the piece of code :
if (Input.Get$$anonymous$$ouseButtonDown(0)){ // if left button pressed...
RaycastHit hit;
Debug.Log("Step1");
Ray ray = its$$anonymous$$inimapCamera.ScreenPointToRay(Input.mousePosition);
Debug.Log("Step2");
if (Physics.Raycast(ray, out hit) ){
Debug.Log("Step3");
// hit.point contains the point where the ray hits the
// object named "$$anonymous$$ap"
Debug.Log(hit.point);
}
its$$anonymous$$ainCamera.transform.position = Vector3.Lerp(its$$anonymous$$inimapCamera.transform.position, hit.point, 0.1f);
}
Wherever I click returns about the same value : (-44.3, 7.7, 5.7) or (-43.2, 8.8, 5.7)
Solved it. I was doing something wrong at the Vector3.Lerp line. So, now works with the more simple method :
if (Input.Get$$anonymous$$ouseButtonDown(0)){ // if left button pressed...
RaycastHit hit;
Debug.Log("Step1");
Ray ray = its$$anonymous$$inimapCamera.ScreenPointToRay(Input.mousePosition);
Debug.Log("Step2");
if (Physics.Raycast(ray, out hit)/* && hit.transform.name=="$$anonymous$$inimapBackground"*/){
its$$anonymous$$ainCamera.transform.position = hit.point;
Debug.Log("Step3");
// hit.point contains the point where the ray hits the
// object named "$$anonymous$$inimapBackground"
Debug.Log(hit.point);
}
//its$$anonymous$$ainCamera.transform.position = Vector3.Lerp(its$$anonymous$$ainCamera.transform.position, hit.point, 0.1f);
}
Quick question though. Can I do something about the very quick movement adjustment that the main camera goes through ? (my main camera is forced by a script to stay at a certain distance and height from the scene). Can I somehow pass only the X axis of the raycast ?
Is your side-scroller going along the X-Y plane? So you don't want the main camera to change in the Z? EDIT: Re-read your question, or change in the Y, yes?
If so, you can set your main camera position like so:
its$$anonymous$$ainCamera.transform.position = new Vector3(hit.point.x, transform.position.y, transform.position.z);
Yes. It worked like this. Now I have one last problem... There is an area around my $$anonymous$$imap where if I click it, it considers as I would've clicked on the $$anonymous$$imap though there is no viewport there. This area is equally extended to the left and right of the $$anonymous$$imap but not above and under.
How is it possible that even though the viewport has a certain area, the raycast detects it as being larger ?
I'm not sure how you have the scene setup, but if you aren't first deter$$anonymous$$ing if the mouse click is within your viewport, your script will always attempt the raycast whenever the left mouse button is down, even if the mouse click is outside the viewport. And if your collider in the scene extends past the edges of your viewport, then you might pick up those clicks.
An easy way to deter$$anonymous$$e if the mouse click is within the bounds of your viewport, then do the raycast if so, would be (and this assumes the following script is attached to the $$anonymous$$imap camera):
if (Input.Get$$anonymous$$ouseButtonDown(0) && camera.pixelRect.Contains(Input.mousePosition))
{
....
}
Answer by Doeko · Aug 18, 2013 at 03:09 PM
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.
Yes, I know about this method. The only problem is that I am using a camera to draw the $$anonymous$$imap and not a texture :(
I am changing a bit the main description, to make it more clear.
Answer by Romeyo · Feb 08, 2020 at 02:11 PM
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.
Your answer
Follow this Question
Related Questions
Change Player MiniMap Icon 1 Answer
Minimap Camera Question 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
how to controll second camera (MiniMap)? 1 Answer
Network restrictive rendering 1 Answer