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 /
  • Help Room /
avatar image
0
Question by kir1251 · Mar 15, 2020 at 01:40 PM · cameraeditormousescreentoworldpoint

ScreenToWorldPoint works wrong after updating to 2019.3.5

Hello. I'm using ScreenToWorldPoint in editor with 2d camera for adding custom ways to manipulate objects on scene. It worked properly until update (I used 2019.3.0 before), now it gives wrong position — it match mouse position in top left corner, but is shifted more the more I move mouse to bottom right corner (see attached images).

The code I'm using for that:

   var sceneCamera = SceneView.lastActiveSceneView.camera;
   var sceneHeight = SceneView.lastActiveSceneView.camera.pixelHeight;
     
   Vector2 MouseWorldPos = sceneCamera.ScreenToWorldPoint(new 
       Vector3(Event.current.mousePosition.x, sceneHeight - Event.current.mousePosition.y, 0));

Hope someone will help me figure out what changed, because before update this had worked as intended.

bottomright.png (53.3 kB)
topleft.png (56.1 kB)
Comment
Add comment · Show 2
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 JPhilipp · Mar 15, 2020 at 03:03 PM 0
Share

@kir1251 What scale do you have in the Play window when running?

avatar image kir1251 JPhilipp · Mar 15, 2020 at 07:51 PM 1
Share

@JPhilipp do you mean the "Game" window? It's ~0.42. But, however, when I changing the sclale, the mouse movement is working the same way (even the offset part is still the same).

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by a436t4ataf · Mar 22, 2020 at 03:21 AM

UPDATE: ScreenToWorldPoint wasn't resolving correctly for me because I was calling it during the wrong phase of Event.type.

To answer your question, I believe you need to use:

 var MouseScreenPosition = HandleUtility.GUIPointToScreenPixelCoordinate( e.mousePosition );

...this automatically adjusts for two UnityEditor "features" added in recent years: Retina support (which makes all coordinates in all existing code and all games ... wrong), and Unity's flip-the-Y bug, where they fail to take account of their own flipping in several of their APIs.

Comment
Add comment · Show 3 · 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 Bunny83 · Mar 22, 2020 at 05:06 AM 0
Share

There is no "flip-the-Y bug" that I know. You may just confuse the different coordinate spaces. ScreenSpace has it's origin at the bottom left corner while GUI space has it's origin at the top left corner. Event.mousePosition is in GUI space while Input.mousePosition is in screen space.

avatar image a436t4ataf Bunny83 · Mar 22, 2020 at 05:54 AM 0
Share

They have multiple different semi-defined versions of what "screen" space is. Even the "Screen" API doesn't work reliably (it has some weird tricks that are only docuemnted in Forums where some of the Unity engineers have provided info - like the weird stuff it does inside OnInspectorGUI, but not always)

Right now, latest docs, official: https://docs.unity3d.com/ScriptReference/Input-mousePosition.html : "The bottom-left of the screen or window is at (0, 0)"

https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html: "Screenspace is defined in pixels. The bottom-left of the screen is (0,0)"

This stuff has been bugged in Unity since I started development back in 3.x days. It's well known, along with things like the screen position being misreported by 20 pixels on some OS's but not all because the Editor API's miscalculated the window size ... and only some of the Unity API's got fixed, so you had to memorise which ones worked and which ones needed correction.

avatar image rasicz · May 07, 2020 at 11:01 PM 0
Share

Thanks, saved me a lot of time.

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

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

How to calculate ui image size in world space? 0 Answers

Why does my mouse cursor flicker in the Unity Editor when dragging any item? 0 Answers

Changing camera angle and screentoworld 1 Answer

Tile based mouse cursor 1 Answer

Any workaround for 5.6 additional camera preview crash (in VR) 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