Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DoubleDouble · May 05, 2011 at 01:47 AM · editorraycasteditor-scripting

Raycasting in Unity Editor

I want to make sure that it's possible to ray-cast inside an editor script. I'm trying to debug this code; just in case someone sees an obvious mistake.

public class MouseEvent : Editor { public GameObject newTile;

 void OnSceneGUI()
 {
     if (Event.current.type == EventType.MouseDown)
     {
         Ray ray = Camera.main.ScreenPointToRay(Event.current.mousePosition);
         RaycastHit hit = new RaycastHit();
         if (Physics.Raycast(ray, out hit, 1000.0f)) {
         Debug.Log(Event.current.mousePosition);
         Vector3 newTilePosition = hit.point;
         Instantiate(newTile, newTilePosition, Quaternion.identity);
         }
     }
 }

}

EDIT: Not sure what changed, but it started working. One thing I'm noticing is that because it's going off of the main camera instead of the editor camera I think it's missing just a little bit. How would I refer to the editor camera to do a ScreenPointToRay?

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 Joshua · May 05, 2011 at 03:20 AM 0
Share

Camera.current perhaps?

avatar image DoubleDouble · May 05, 2011 at 07:30 PM 0
Share

Yea, lol ;) thx.

3 Replies

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

Answer by DoubleDouble · May 05, 2011 at 07:40 PM

It's fine to do Raycasting in the editor; remember if you do screen point to ray to use Camera.current instead of Camera.main and it will use the editor camera as long as the editor screen has focus.

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
14

Answer by sbsmith · May 26, 2016 at 07:53 PM

In Unity 5, I found that Camera.current didn't quite work (I saw the y coordinate flipped). Instead I used HandleUtility.GUIPointToWorldRay

I found this unusual since the documentation says that it uses the current camera to do the calculation.

         // Called from OnSceneGUI in a subclass of Editor
         // Camera.current did not work
         //Ray ray = Camera.current.ScreenPointToRay( Event.current.mousePosition );
         Ray ray = HandleUtility.GUIPointToWorldRay( Event.current.mousePosition );
         
         RaycastHit hit;
         if( Physics.Raycast( ray, out hit ) )
         {
             // do stuff
         }

Comment
Add comment · Show 4 · 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 giocchan · Oct 28, 2017 at 03:37 PM 0
Share

Thank you VERY much

avatar image Skegon · Oct 26, 2018 at 09:30 AM 0
Share

Thank you. I tried getting the ray through ScreenPointToRay() on the sceneView camera, but both gave weird results. This, however, worked perfectly.

avatar image Bunny83 Skegon · Oct 26, 2018 at 10:30 AM 1
Share

ScreenSpace has its origin at the bottom left corner while GUI space has its origin at the top left. In addition GUI spaces can be nested. So the event information may be relative to a sub area. "GUIPointToWorldRay" does "unclip" the local gui position and transforms it internally into screenspace and finally to a ray.


You may want to have a look at the HandleUtility as it has several other utility methods for editor program$$anonymous$$g including several ClosestPoint methods, "PickGameObject", "PickRectObjects" as well as the RaySnap method. Note that Physics.Raycast can only hit "colliders". The selection / pick methods can pick up any object based on its renderer or other things. If you're implementing an editor tool you usually want to use those ins$$anonymous$$d of Physics.Raycast unless you specifically need a collider and the hit information.

avatar image sbsmith Bunny83 · Nov 04, 2018 at 05:42 AM 0
Share

If you're interested in casting rays in the editor without needing a collider, check out this post https://forum.unity.com/threads/editor-raycast-against-scene-meshes-without-collider-editor-select-object-using-gui-coordinate.485502/

avatar image
0

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

You could also do it through SceneView, but you need to flip the y-coordinate upside down.


SceneView also has an issue with the top ribbon-menu, which can therefore offset your coord down by 18 pixels, making it appear as an incorrect, unexpected value.


Here is the 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

"add selected" editor script 1 Answer

Draw specific Object Inspector into Rect 1 Answer

Editor Window Views 0 Answers

Cancel build in PostProcessScene? 0 Answers

How can I open the Sprite Editor from script? 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