Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Korenski405 · Apr 14, 2012 at 05:55 AM · mousecoordinates

Having trouble getting Custom Editor to get a world point.

Alright, I have a Grid script with a public Vector3 variable called mouseWorldPoint, and I want to update its position as I move about the Sceneview window.

Here is my code so far in the OnInspectorGUI() method:

 if(SceneView.lastActiveSceneView.camera != null)
 {
   Raycasthit hit;
   Ray worldRay = SceneView.lastActiveSceneView.camera.ViewportPointToRay(Event.current.mousePosition);
   Physics.Raycast(worldRay, out hit);
   grid.mouseWorldPoint = hit.point;
 }

I wopuld have thought this would have done the trick, but it does not seem to be working. Any thoughts?

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

4 Replies

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

Answer by Remi_Tribia · May 06, 2015 at 03:36 PM

Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);

It works perfectly, I spent half a day to figure this out!

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 Eric5h5 · Aug 30, 2015 at 06:59 PM 3
Share

Specifically,

 var mousePosInScene = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition).origin;
avatar image
7

Answer by kUr4m4 · Nov 15, 2013 at 10:42 AM

To get the current mouse position in world coordinates in the scene view I do the following:

 Vector2 mousePos = Event.current.mousePosition;
 mousePos.y = sceneView.camera.pixelHeight - mousePos.y;
 Vector3 position = sceneView.camera.ScreenPointToRay(mousePos).origin;

While mousePosition reads coords from bottom to top, world coordinates are read top to bottom. Hence the use of the pixemHeight - mousePositionY.

Also note the use of pixelHeight rather than Screen.Height. This is due to the fact that, in the scene view, Screen.Height takes into account the bar above the actual window, causing a little deviation on the actual position on the screen.

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 hamstar · Feb 25, 2014 at 04:33 PM 0
Share

I know this is an old post, but thank you! Could not work out why my position was off. Adjusting for the deviation like you said fixed it.

avatar image
1

Answer by rutter · Apr 14, 2012 at 07:31 AM

Looks like that SceneView class isn't in the official docs, but some quick Googling turns up a number of people trying to use it to access the scene camera; some people have also tried Camera.current, which I believe I've done once or twice in the past.

In this question thread, someone was trying to raycast from the scene camera and getting some very unusual results. You might check to make sure that you are firing a ray, and that it's going roughly where you expect.

If you really can't get this to work, I've had some luck before using `Handles.PositionHandle()` to get and set a point in scene.

Some other reading that might be useful:

  • http://forum.unity3d.com/threads/104281-Editor-Script-Raycast-in-editor-problem-and-selection-woe-s

  • http://answers.unity3d.com/questions/62655/raycasting-in-unity-editor.html

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
avatar image
0

Answer by IgorAherne · Oct 07, 2018 at 08:44 PM

Necro, but important because pops-up in google search.


You use ViewportPointToRay incorrectly - it should accept a vector with components between 0 and 1, for example new Vector2(0.015f, 0.95f);. Notice, that Event.current.mousePosition will have values like (1249, 123) etc, which clearly won't fit into the 0-to-1 range.

You can squash it into that range, by comparing it to the width and height of your window (or your sceneView). For example (eventCurrMousePos.x / sceneView.position.width) and (eventCurrMousePos.y / sceneView.position.height)


Also, you need to flip Event.current.mousePosition before using it. Notice, SceneView has a ribbon-menu which occupies 30 to 18 pixels depending on your screen. This makes many people freak out and blame sceneView for producing incorrect values. Here is a fix: https://forum.unity.com/threads/mouse-position-in-scene-view.250399/#post-3760579

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Find Mouse co-ordinates on click in a 2d environment 5 Answers

Transform's position wherever the mouse is pointing. 2 Answers

getting world coordinates of mouse position 2 Answers

How do I get the x and y coordinates out of Input.mousePosition? 2 Answers

Coordinates of mouse in a box collider 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