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
3
Question by TheJayyBe · Jan 02, 2013 at 12:52 AM · input.mousepositioncamera.screentoworld

ScreenToWorldPoint returning weird value.

Hi, I am trying to print the clicked point in global point. But it always returning the same value, no matter where I click: X: 5.828779, Y: 0. I don't have idea what to try. I need this values to do some stuff, and I lost my day trying. Thanks for any help.

  void Update () {    
              
             
          if (Input.GetMouseButtonDown(0)) {
                 
                 Vector3 cameraVector = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             print("Posição X: " + cameraVector.x);
                         print("Posição Y: " + cameraVector.y);
             }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Eric5h5 · Jan 02, 2013 at 01:14 AM

It's actually not weird, it's expected, since you haven't given it any depth value, so it's always going to return the camera position. You need something greater than 0 for the z element of the Vector3 in ScreenToWorldPoint.

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 Bunny83 · Jan 02, 2013 at 01:25 AM 0
Share

Right, that means since you passed a Vector2 variable to a Vector3 parameter, Unity will implicit convert the Vector2 into a Vector3 by setting the missing 3rd component to 0.

So your mouse position (x,y) becomes (x,y,0). Like you can read in the docs the z value deter$$anonymous$$es the distance from the camera.

avatar image Eric5h5 · Jan 02, 2013 at 02:10 AM 0
Share

Actually Input.mousePosition returns a Vector3, with z=0. Although that's certainly true about the Vector2 implicit conversion (and that's what would happen if you used Event.current.mousePosition, which does return a Vector2).

avatar image TheJayyBe · Jan 02, 2013 at 10:10 PM 0
Share

It worked, I put for example z=2 and he gave me the points when clicked in update. But I don't know why it does not work with z=0, and give me the same values no matter where I click because z=0 2D space has global values too. Is this means that if I want to know point in z=0, it will not work?

Actual code:

void Update () {

  if (Input.Get$$anonymous$$ouseButtonDown(0)) {            
         Vector3 vetor3 = new Vector3(Input.mousePosition.x,Input.mousePosition.y,-1);
         Vector3 cameraVector = Camera.main.ScreenToWorldPoint(vetor3);
     print("Posição X: " + cameraVector.x);
                 print("Posição Y: " + cameraVector.y);
     }

}

avatar image Eric5h5 · Jan 03, 2013 at 03:33 PM 0
Share

As I mentioned, 0 = camera position. If the distance you're trying to get is 0 units away from the camera, it doesn't matter where the mouse position is, it's mathematically impossible for it to be anything other than the camera position. You need to provide at least a tiny bit of depth.

avatar image
2

Answer by UsefulYdyot · May 17, 2020 at 09:47 PM

For 2D at least you can pass negative camera z position as z. This is what worked for me.

 cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -cam.transform.position.z));

Took me long to understand this. Thanks for the hints in the answers of this question.

Comment
Add comment · Show 1 · 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 NivaBento · Jun 04, 2020 at 06:34 AM 0
Share

my 2cents to complete the 2D:

  Vector3 mouseWzPos = new Vector3(Input.mousePosition.x, 
                 Input.mousePosition.y, Camera.main.transform.position.z);
 Vector3 mousePos = Camera.main.ScreenToWorldPoint(mouseWzPos);
 
 mousePos.z = 0;

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

Scale Z Axis Movement According to Camera Tilt (Drag Control Camera) 0 Answers

Controlling object movement with mouse 0 Answers

Input.mousePosition only detects motion outside of window if I move really slow. 1 Answer

Zoom to center of Pinch Gesture 1 Answer

my script not working to move plane to mouse position 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