Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
11
Question by THEpancakes · Oct 12, 2012 at 08:16 PM · screentoworldpointinput.mouseposition

ScreenToWorldPoint() not working

Hi,

I'm currently experimenting with ScreenToWorldPoint(), and something just doesn't seem to be working right...

Whenever I use this code:

 function Update() {
 Debug.Log(camera.ScreenToWorldPoint(Input.mousePosition));
 }

It just shows (0.0 ,125.0, 0.0) all the time in the Debug console, regardless of how much I move the cursor around the screen.

How can I make my code display the actual world coordinates my mouse is currently passing over?

Thanks in advance. :)

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

Answer by aldonaletto · Oct 12, 2012 at 09:03 PM

ScreenToWorldPoint receives a Vector3 argument where x and y are the screen coordinates, and z is the distance from the camera. Since Input.mousePosition.z is always 0, what you're getting is the camera position.
The mouse position in the 2D screen corresponds to a line in the 3D world passing through the camera center and the mouse pointer, thus you must somehow select which point in this line you're interested in - that's why you must pass the distance from the camera in z.
If you try something like this:

 function Update() {
   var mousePos = Input.mousePosition;
   mousePos.z = 10; // select distance = 10 units from the camera
   Debug.Log(camera.ScreenToWorldPoint(mousePos));
 }

you will get the world point at 10 units from the camera.

Comment
Add comment · Show 6 · 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 THEpancakes · Oct 13, 2012 at 09:07 PM 0
Share

thanks, that worked perfectly :)

avatar image Fattie · Mar 11, 2014 at 04:17 PM 0
Share

don't forget ut's different when you are orthographic!

avatar image felixpk · Apr 15, 2014 at 12:54 PM 0
Share

Hey it still does not work for me :(

        BoxCollider2D destroyBox =   Camera.main.GetComponent<BoxCollider2D>();
         colliderPos = camera.ScreenToWorldPoint(new Vector3(0, 0, -10));
 
         Vector2 posXY = new Vector2(colliderPos.x,colliderPos.y);
         destroyBox.center = posXY;

I want the BoxCollider2D to be on the left side of the Camera, all it does is positioning it about 1 unit left from the center of the camera... anyone an idea?

avatar image Sarseth · Jul 19, 2015 at 11:17 AM 0
Share

Almost. But it works only when player is standing in place. I'm moving camera with player, and then it does not work. This is not the true position of the mouse in the map. Only relative to center.

avatar image manhngovan · Apr 04, 2017 at 10:37 AM 0
Share

Thank you, @aldonaletto! I'm newbie. Your answer is the only one that make me understand how to use ScreenToWorldPoint. I don't understand until see your answer. I don't know where to like or give you 1point I'm having.

Show more comments
avatar image
3

Answer by Eric5h5 · Oct 12, 2012 at 08:52 PM

You have to specify a Z position, like the code example in the docs does.

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

20 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

Related Questions

Trying To Find Mouse Position On Tap 1 Answer

Converting Input.mousePosition to world coordinates 1 Answer

Find TextureCoordinates from plane with out raycast only using mouse position on plan 0 Answers

Game object moving in opposite direction instead of same direction while dragging it 0 Answers

World point for MousePosition 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