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
0
Question by FreeFly90 · May 28, 2016 at 05:13 AM · editorraycastmousehitscreenpointtoray

Raycast hit done wrong using ScreenPointToRay coordinates

Hello guys, after two days of search I finally decided to give up on my attempts to make this work and I decided to ask for help from those who most certainly know more about me :P What I am trying to achieve is simple in its core mechanics, but still is driving me mad: my final goal is to change some objects attributes by clicking on them in the Scene Editor window, so far I managed to make it work only on some clicks, but most of the times it works even when I do not click the right position.

So far I have a simple plane mesh (imported from blender), with a script attached to it that makes it blue when I click it. The plane is scaled on both x and y axis and has a box collider that fits the mesh (checked both in ortho and perspective mode):

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [ExecuteInEditMode]
 public class TileHandler : MonoBehaviour {
     public void colorChange () {
         GetComponent<Renderer>().sharedMaterial.color = Color.blue;
         Debug.Log("Color changed");
     }
 }
 

In the Editor folder I have another script that checks whether the mouse click hits the target or not, and call the proper method in case.

 using UnityEngine;
 using UnityEditor;
 
 [InitializeOnLoad]
 [CustomEditor(typeof(TileHandler))]
 
 public class MouseHelper : Editor
 {
     //static TileHandler tile;
      //public override void OnInspectorGUI() {
         //tile = (TileHandler) target;
     //}
     
     static MouseHelper()
     {
         SceneView.onSceneGUIDelegate += UpdateView;
     }
  
     private static void UpdateView(SceneView sceneView)
     {
         if (Event.current.type == EventType.mouseDown){
             RaycastHit hit; 
             Ray ray = Camera.current.ScreenPointToRay(Event.current.mousePosition); 
             if(Physics.Raycast(ray, out hit, 20000)){
                 TileHandler g = (TileHandler) hit.transform.gameObject.GetComponent<TileHandler>();
                 Undo.RecordObject(g.GetComponent<Renderer>().sharedMaterial, "Undo Color Change");
                 g.colorChange();
                 EditorUtility.SetDirty(g);
             }
         }
     }
 }

I know the code is not optimized, but i was just trying to make it work. Can you spot what am I doing wrong? The scene is a 3D one.

Comment
Add comment · Show 1
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 Eudaimonium · May 29, 2016 at 10:12 AM 1
Share

It could be that the first time you click anywhere not on the actual object, the script throws an exception because "g" variable will be null in that case, and from there the script will not be executed any more.

Do you see any red errors in the console?

Add a check to see, if (g != null) immediately after GetComponent line just in case.

2 Replies

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

Answer by Bunny83 · May 29, 2016 at 01:22 PM

You mix GUI coordinates with Screen coordinates. Screen coordinates start at the bottom left while GUI coordinates start at the top left. So don't use "Event" data which belong to GUI space in ScreenPointToRay.

ScreenPointToRay should be used at runtime with Input.mousePosition which is in ScreenSpace and does start at the bottom left.

In the editor however you should simply use HandleUtility.GUIPointToWorldRay which does pretty much the same as "ScreenPointToRay" but works with GUI coordinates.

You might also want to have a look at HandleUtility.PickGameObject which is able to pick objects which don't have a collider attached. This is what the editor uses for it's native object selection.

Apart from that you really should consider what @Eudaimonium said in the comment above. If you click on an object that doesn't have your "TileHandler" script attached your code will throw a NullReference exception.

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 FreeFly90 · Jun 03, 2016 at 06:03 PM

Thanks, this is exactly what I needed, I was pretty sure I was messing up with the API, but I couldn't find the right function by myself. Right now I only have a grid made of these objects that are supposed to change color on click in the editor, now that I fixed it I'll continue with the procedural level generation. I will definitely follow both yours and @Eudaimonium suggestions, thanks a lot guys!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

[2D] Raycast to mouse cursor = offset on Y axis 0 Answers

Physics.Raycast sometimes does not work? 0 Answers

Scripting a Click event in the Unity Editor 1 Answer

How do you use a queuing mechanism in Unity? 1 Answer

Network - Editor + Builds = Problem with RayCasts? 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