Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by Prosto_Lyubo · Jan 10, 2016 at 03:57 AM · uicanvasmathunitsvector3.distance

Measuring distance on Camera Screen-Space Canvas in -canvas units-

So: I have a canvas which is set to Screen Space - Camera. I have there (as its child) a Sprite (which is at a different angle than the canvas) and an Image. So the hierarchy looks like that:

-Canvas
--Sprite
---Image

I have a code that allows user to drag the image on the Sprites plane. The issue is, that I don't want the user to be allowed to drag the Image onto the Sprite. So I measure the distance between them (via Vector3.Distance), and don't update the Images transform if the distance is below some constant threshold. Everything works fine until I maximize "Game" window or change the resolution. Physical distance my threshold represents is different depending on the Canvas Scaling. So how I measure the distance between two objects on the Canvas in "Canvas units"? How to solve this problem in a neaty way?
I tried the constant pixel size and "scale with screen". I Also tried multiplying the distance by the Canvas.scaleFactor. None of this helps. Sure I can do this with Colliders and triggers but I don't want to produce unnecessary garbage. How does UnityEngine.UI class handle this? Sliders works fine on scalable canvas... Can You help me?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Prosto_Lyubo · Jan 11, 2016 at 01:03 PM

I found the solution, not as clean as I wanted but still better than dealing with unnecessary colliders. Since this question have so many followers I'll post it for You. I added two RectTransform objects 'RefA' and 'RefB' separated by 50 pixels (which corresponds to one Unity unit according to my sprites Import Settings). They both are children of my Canvas. Then I created property

 private float DistanceUnit { get { return Vector3.Distance( RefA.position, RefB.position ); } }

It can also be made static and pushed to some Utility class if You want to use it in other classes. Last step is to divide any calculated distance by DistanceUnit value. Here is an example to above question:

 var distance = Vector3.Distance( Image.transform.position, Sprite.transform.position ) / DistanceUnit;
Comment
Add comment · 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 Nido · Aug 19, 2016 at 07:29 AM 1
Share

First, thanks for post your solution. This is a good practice for helping even in non answered questions. Second, maybe Camera.WorldToScreenPoint could help in this kind of situations where you have to measure two points "as you see them" in the screen. This is helpful even for measuring distances between 3d objects in scene and GUI stuff. Not sure if this is the best solution for you issue, but may help ;)

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.

Follow this Question

Answers Answers and Comments

42 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

Related Questions

Spot the difference game having a strange behavior in Mobile 0 Answers

Indicating object outside camera screen view 0 Answers

Is there any way to disable post processing for a gameobject? 0 Answers

Canvas screen space overlay/camera. Problems. 1 Answer

World Space UI as an in-game computer screen! 0 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