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 MrLight · Apr 06, 2013 at 04:18 AM · editorunityeditorrayscreenpointtoray

Editor mousePosition changing when key is pressed

I am trying to instantiate an a gameObject on my grid when I press 'a'. When 'a' is pressed it creates a ray from where the mouse is on the sceneview and gets where it hits the xz-plane. This is the point I wish to place the object. The log shows it is correct when I am moving my mouse over the grid but when I press 'a' it seems to change the point to a different location. Anyone know whats going wrong with this code?

 void GridUpdate(SceneView sceneview) {
         Event e = Event.current;
     
         Ray r = sceneview.camera.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + sceneview.camera.pixelHeight,0));
         Plane p = new Plane(new Vector3(0,0,0), new Vector3(0,0,1), new Vector3(1,0,1));
         float d=0f;
         p.Raycast(r,out d);
         Vector3 pPoint = r.GetPoint(d);
         //Debug.Log(pPoint); // This gives correct point until 'a' is pressed
     
     
         if (e.isKey && e.character == 'a') {
     
             Object prefab = PrefabUtility.GetPrefabParent(Selection.activeGameObject);
              
             if (prefab) {
               obj = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
     
               Vector3 aligned = new Vector3(Mathf.Floor(pPoint.x/map.space) + map.space/2.0f,0,
                                     Mathf.Floor(pPoint.z/map.space) + map.space/2.0f);
     
               
               obj.transform.parent = map.transform;
               obj.transform.position = aligned;
             }
           
         }
       }
Comment
Add comment · Show 3
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 MrLight · Apr 07, 2013 at 06:49 PM 0
Share

After playing around with this. It seems the mousePosition.x is correct and its just something to do with -e.mousePosition.y + sceneview.camera.pixelHeight. I avoided this problem by using if (Event.current.type == EventType.mouseDown) ins$$anonymous$$d.

avatar image sdgd · Apr 07, 2013 at 07:50 PM 0
Share

why is it:

 Ray r = sceneview.camera.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + sceneview.camera.pixelHeight,0));

why isn't:

 Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);

I don't get it why scene view as in scene you won't get running code unless you have run in backeground

and why are you using event for it use update and not OnGUI

is there a special reason for using OnGUI?

avatar image MrLight · Apr 07, 2013 at 09:30 PM 0
Share
 Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);

Two reason for not using this are the main camera is not the camera the editor scene windows use and input class only works in play. To get it to work in the editor you need to use Camera.current or the correct sceneview camera and use Event.current to get input.

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

2017.3 Editor frame drop and movement reaction speed [ISSUE] 1 Answer

Debug Console doesnt show up 7 Answers

Is there a way to create a folder hierarchy view in the editor? 1 Answer

Creating a custom script that extends other scripts in Editor 1 Answer

Using Camera screen point to ray for look rotation problem... 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