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 BondoGames · Jan 20, 2018 at 11:31 PM · custom editorcustom inspector

Custom Inspector Raycasting

After some time of googling, I've failed to find anything that helps me on this. I've got a script that needs to detect if the player has clicked on a state or not, but the Debug.Log() I have inside of the Physics.Raycast is never getting called. I can't seem to find the problem.

 void OnSceneGUI () {
         if (grabbingState) {
             int controlID = GUIUtility.GetControlID (FocusType.Passive);
             HandleUtility.AddDefaultControl (controlID);
             if (Event.current.type == EventType.MouseDown) {
                 if (Event.current.button == 0) {
                     Debug.Log ("Help");
                     Ray ray = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition);
                     RaycastHit hitInfo;
                     if (Physics.Raycast (ray, out hitInfo, 1000f)) {
                         Debug.Log (hitInfo.collider.name);
                         if (hitInfo.collider.tag == "State") {
                             Debug.Log ("Hit state");
                             //localState.ConnectedStates.Add (hitInfo.collider.gameObject.GetComponent<State> ());
                         }
                     }
                 }
                 Event.current.Use ();
                 grabbingState = false;
             }
         }
     }

Any and all help is appreciated, Cheers.

EDIT: I just thought to include a note - The game is 2D

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
Best Answer

Answer by WinterboltGames · Jan 20, 2018 at 11:48 PM

At line 8, try Camera.current.ScreenPointToRay(Input.mousePosition);

Camera.current will refer to the editor's camera then...

EDIT: It's 2D Right? then try something like this...

Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit2d = Physics2D.Raycast(ray.origin, ray.direction);

EDIT: Try this... Ray ray = HandleUtility.GUIPointToWorldRay( Event.current.mousePosition ); RaycastHit2D hit2d = Physics2D.Raycast(ray.origin, ray.direction);

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 BondoGames · Jan 20, 2018 at 11:55 PM 1
Share

It's returning the hit2d collider as null (that it hit nothing) when I click on sprites that have colliders, any thoughts?

avatar image BondoGames · Jan 21, 2018 at 12:00 AM 1
Share

Your last edit works perfectly!

avatar image WinterboltGames BondoGames · Jan 21, 2018 at 12:01 AM 0
Share

happy creating...

avatar image BondoGames WinterboltGames · Jan 21, 2018 at 12:17 AM 1
Share

Thanks, I had tried that before but without the 2D part - I hadn't realized raycast changes in 2D

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

76 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

Related Questions

Dictionary becomes null unexpectedly when using a custom inspector 2 Answers

Can the GameObjectInspector window be customized? 0 Answers

Inspector Overlapping Text Label at a Position 1 Answer

Custom editor window does not show prefab overrides 2 Answers

Bool field in CustomPropertyDrawer isn't clickable? 2 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