• 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
0
Question by bwaite87 · Jul 01 at 01:35 AM · mapcoordinatesworldspace

Instantiate gameobject at world position - worldPOS found from clicking on Raw Image

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?

Comment
Add comment · 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 tormentoarmagedoom · Jul 01 at 07:37 AM 0
Share

I'm not sure.. Is possible the map must be at the center of the camera when changing orth. size?. What you could do i supose i that the screen shows 2 cameras at same time, the upper camera have the map at its center, and when the player wants to scroll, the map centers itself... Somethign like this.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bwaite87 · Jul 03 at 12:18 AM

After SO much time I was able to get the help from someone on Discord that helped me solve this problem.

I will post their answer for any future people having this issue:


I was able to reproduce your issue. The problem lies somewhere in your adjustments between getting the inital click coordinates and converting to worldpoint. Seeing potential optimizations I didn't look into it much further and made this code that does the same:

 RectTransformUtility.ScreenPointToLocalPointInRectangle(_screenRectTransform, eventData.position, null, out Vector2 localClick);
 Vector2 viewportClick = new Vector2(localClick.x / _screenRectTransform.rect.xMax, localClick.y / _screenRectTransform.rect.yMax);
 Vector3 worldClick = mapCamera.ViewportToWorldPoint(viewportClick);
 Instantiate(pinkPixel, worldClick + Vector3.forward, Quaternion.identity);

you also need to do one more thing in the editor, namely to change the Pivot on the RectTransform of your RawImage. This makes it so you don't need to adjust for wierd signums (the part where the mistake lies)

Comment
Add comment · 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

140 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 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 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 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 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 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 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 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

Assigning UV Map to model at runtime 0 Answers

Drawing an overlay mini-map 6 Answers

UI issue: Getting screen coordinates from a World Space object 1 Answer

Gui Texture to display Map coordinates 0 Answers

My ingame avatar's worldspace does not coincide with other objects 1 Answer


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