Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
3
Question by hazarartuner · Jun 04, 2017 at 12:12 PM · editor-scriptingscreentoworldpoint

Unity Editor mouse position to world point

Hi,

I wanna create an editor script. With that, user can add some assets to the scene by clicking on the screen. (NOT IN GAME, IN EDITOR) To do this, I need to find the mouse position from SCENE CAMERA to world point. I did some research and made a POC. I pass the best answer I could found. But the the problem is, it is not working, it looks like an exponential offset happening to the world position. I don't know what the problem is and there is no clear answer I could found in the internet. Could some one help me?

Here is my Editor Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(LevelManager))]
 public class LevelManagerEditor : Editor {
     SerializedProperty position;
 
     void OnEnable()
     {
         position = this.serializedObject.FindProperty("position");
     }
     
     public void OnSceneGUI(){
         Vector2 mousePos = Event.current.mousePosition;
                 mousePos.y = Camera.current.pixelHeight - mousePos.y;
         
         Vector3 worldPosition = Camera.current.ScreenToWorldPoint(mousePos);
                 worldPosition.z = 0;
 
         position.vector3Value = worldPosition;
 
         serializedObject.ApplyModifiedProperties();
     }
 }
 

Here is Game Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LevelManager : MonoBehaviour {
     public Vector3 position;
 
     void OnDrawGizmos()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawCube(position, Vector3.one * .5f);
     }
 }
 




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
7

Answer by hazarartuner · Jun 04, 2017 at 04:31 PM

I found the solution. It was 2D game and I found the answer in this post: http://www.yudiz.com/making-a-basic-2d-tile-map-editor-using-a-custom-inspector/

TLDR:

I get the position by: HandleUtility.GUIPointToWorldRay (Event.current.mousePosition).origin and set "z" coordinate to 0.

Comment
Add comment · Show 2 · 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 TwilingWay · Oct 17, 2018 at 11:29 AM 0
Share

Hi, Can you give right varriant?

avatar image Maximus_a · Feb 02, 2021 at 09:53 PM 0
Share

thank you!

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

116 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 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

ScreenToWorldPoint with CanvasScaler and cam childed to a GO 1 Answer

How to change Texture in editor 0 Answers

Any way to set AnimatorOverrideController's animation clips programatically? 1 Answer

How to get mouse click world position in the scene view in editor script? 3 Answers

Error loading auto-generated monobehavior at run-time 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