• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by primus88 · Aug 16, 2013 at 09:09 PM · cameraminimap

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.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image primus88 · Aug 18, 2013 at 02:39 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
primus88
AlexKostas
sanchez_x

People who like this

3 Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image primus88 · Aug 18, 2013 at 07:40 PM 0
Share

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.GetMouseButtonDown(0)){ // if left button pressed...
     RaycastHit hit;
             Debug.Log("Step1");
     Ray ray = itsMinimapCamera.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 "Map"
                 Debug.Log(hit.point);
     }
             itsMainCamera.transform.position = Vector3.Lerp(itsMinimapCamera.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)

avatar image primus88 · Aug 18, 2013 at 08:50 PM 0
Share

Solved it. I was doing something wrong at the Vector3.Lerp line. So, now works with the more simple method :

 if (Input.GetMouseButtonDown(0)){ // if left button pressed...
 RaycastHit hit;
             Debug.Log("Step1");
 Ray ray = itsMinimapCamera.ScreenPointToRay(Input.mousePosition);
             Debug.Log("Step2");
 if (Physics.Raycast(ray, out hit)/* && hit.transform.name=="MinimapBackground"*/){
                 itsMainCamera.transform.position = hit.point;
                 Debug.Log("Step3");
   // hit.point contains the point where the ray hits the
   // object named "MinimapBackground"
                 Debug.Log(hit.point);
 }
             //itsMainCamera.transform.position = Vector3.Lerp(itsMainCamera.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 ?

avatar image whebert · Aug 18, 2013 at 10:31 PM 1
Share

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:

 itsMainCamera.transform.position = new Vector3(hit.point.x, transform.position.y, transform.position.z);
avatar image primus88 · Aug 19, 2013 at 10:26 AM 0
Share

Yes. It worked like this. Now I have one last problem... There is an area around my minimap where if I click it, it considers as I would've clicked on the minimap though there is no viewport there. This area is equally extended to the left and right of the minimap 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 ?

avatar image whebert · Aug 19, 2013 at 06:55 PM 1
Share

I'm not sure how you have the scene setup, but if you aren't first determining 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 determine 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 minimap camera):

 if (Input.GetMouseButtonDown(0) && camera.pixelRect.Contains(Input.mousePosition))
 {
    ....
 }
Show more comments
avatar image

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.

Comment
Rukas90

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image primus88 · Aug 18, 2013 at 03:22 PM 0
Share

Yes, I know about this method. The only problem is that I am using a camera to draw the minimap and not a texture :(

I am changing a bit the main description, to make it more clear.

avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How can I simulate the "f" hotkey in the editor? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges