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
4
Question by pajamajama · Oct 20, 2013 at 07:36 AM · camerainputscreentoworldpointinput.mouseposition

Camera Screen to world point returns cameras transform position.

 Debug.Log (Camera.main.ScreenToWorldPoint(Input.mousePosition));

is just giving me the transform of the camera, no matter where my mouse is on screen. Any ideas? Thank you.

Comment
Add comment · Show 4
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 stepan-stulov · Jan 16, 2015 at 10:41 AM 1
Share

This is such an inconsistency by Unity. They ask the screen position to be supplied with the Z, while for screen position it makes absolutely no sense. I can see the explanations here, but they're rather intuitive, and not mathematically formal. Screen is flat and has no depth and therefore no Z! All the mouse/touch points are already there, and speaking of the world coordinates, are necessarily at the clipping plane! One could argue this is a na$$anonymous$$g problem, that the method could be called ViewportToWorldPoint or smth, but its actually not. They want you to have two of the three coordinates in screen space, and one in the actual world space. How more inconsistent could it be to provide different components of the same Vector3 in different coordinate systems? As much as I love Unity3D, as much its API is a huge chunk of mess. Radians vs degrees, coordinate systems, Vector3 vs Vector2, you name it...

avatar image num1_cyberbud stepan-stulov · Feb 22, 2016 at 08:13 AM 0
Share

So how is this resolved? How do I get ScreenToWorldPoint without passing in the camera z position, and switch in the object's z position?

avatar image neonwarge04 · Feb 22, 2016 at 07:17 AM 1
Share

@Stepan.stulov because unity

avatar image Nullius · Jan 31, 2017 at 02:47 PM 0
Share

Still not working for me, the z is different to the camera's z, but the object still follows the camera's x and y ins$$anonymous$$d of the mouse.

6 Replies

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

Answer by Huacanacha · Oct 20, 2013 at 07:52 AM

As others have pointed out you need Z, which is the distance from the camera. To get the true world point on the cameras viewing plane you should use the clipping plane distance:

 Debug.Log(Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
   Input.mousePosition.y, Camera.main.nearClipPlane)));

I believe if you use 0 you will just get the camera's position, as X and Y are irrelevant when scaled to zero. The viewing plane at the cameras origin is a singularity ;)

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 meat5000 ♦ · Oct 21, 2013 at 04:45 PM 0
Share

If my near plane is 0.2, that would be 0.2 away from Camera position.

avatar image Huacanacha · Oct 21, 2013 at 05:03 PM 0
Share

That's correct... the virtual 'window' you are viewing through is 0.2 in front of the camera if the near clip plane is 0.2. The original problem was that ScreenToWorldPoint was returning the cameras position, which will always be the case when Z is zero.

The viewport is a projection of the rectangular viewing window that exists at nearClipPlane distance forward of the camera. For non-orthographic projections the further you get from the camera the larger this 'window' is in world space, but Everything will look exactly the same (as the camera projection is the same) regardless of the clip plane distance except that objects are cutoff closer or further from the camera.

avatar image Huacanacha · Oct 21, 2013 at 05:23 PM 0
Share

It's implied by the fact that the OP doesn't want the Camera position (this was their problem in the first place) and the fact they're looking for the point on the screen in world space, according to the function they've chosen... the only logical positions for this could be the camera position (not in this case) or the XY point on the near clip plane.

The function you linked, ScreenToViewportPoint, doesn't give the point in world space. It just normalizes input to [0,1] range and flips the vertical axis (ok it also fills in Z which I assume will be the near clip distance).

avatar image meat5000 ♦ · Oct 21, 2013 at 05:25 PM 0
Share

Reverted and cleaned, at your request :)

Your supporting comments make yours the better answer ;)

avatar image Huacanacha · Oct 21, 2013 at 05:33 PM 0
Share

Ok thanks... and also for the discussion ;)

Show more comments
avatar image
3

Answer by pajamajama · Oct 20, 2013 at 07:39 AM

Just found out that you need to supply a z position for it to work. http://answers.unity3d.com/questions/18205/World-mouse-position-not-depending-on-Screen-mouse-position.html

 mousePos = Input.mousePosition;
 mousePos.z = 1.0;
 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
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 Huacanacha · Oct 20, 2013 at 07:54 AM 1
Share

Using 1 for z gives the projected X,Y point at 1 unity from the camera. To get the point on the clipping plane use camera.nearClipPlane. See my full answer below.

avatar image
2

Answer by mandisaw · Jan 13, 2018 at 03:39 PM

The input z-coordinate is the key, but it should actually be equal to the Y-axis distance from the Camera to the object being moved. That distance is only equal to Camera.nearClipPlane if your scene configuration happens to have your scene at the Camera's near clip plane.

 void OnMouseDrag() {
     // ScreenToWorldPoint requires distance to Camera as z-coordinate, otherwise it only returns current, fixed Camera position (distanceToCamera = 0)
     Vector3 screenPosition = Input.mousePosition;
     screenPosition.z = Camera.main.transform.position.y - transform.position.y;
 
     Vector3 worldPosition = Camera.main.ScreenToWorldPoint (screenPosition);
     transform.position = worldPosition;
 }
 
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
1

Answer by meat5000 · Oct 20, 2013 at 07:42 AM

Debug.Log (Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition,0)));

Z element is depth. As Huacanacha says, z = 0 is Camera position and Camera.main.nearClipPlane is the viewport in world space.

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

Did you want

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html

instead?

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 aeroson · Dec 12, 2015 at 11:44 AM

So I had exactly the same code as yours. The bug was that I did set camera position multipler times in multiple scripts. Camera position changed by script X that is should not do, then player target direction computed from camera with ScreenToWorldPoint, then camera position changed to look at player. Result is camera looks at player everything looks fine, but the aiming seems like there is a bug in ScreenToWorldPoint. Tried disabling random scripts and it worked. That way I found out which one does it.

Comment
Add comment · Show 3 · 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 · Dec 12, 2015 at 12:47 PM 0
Share

That doesn't really answer the question. Are you sure you read the other answers? A screen point is a 2d point. When you want to "unproject" it you have to supply a depth value. That's the problem in the code in question.

avatar image aeroson · Dec 12, 2015 at 12:49 PM 0
Share

OP doesnt say if he uses orthographic or perspective camera, in my case (orthographic) the Z seem to not matter.

avatar image Bunny83 · Dec 12, 2015 at 05:09 PM 0
Share

Well, he doesn't say that he's using a perspective camera, however the symptoms he has clearly verifies that he's using a perspective camera since at distance 0 all points map to the camera origin.

Of course the result would be different when you use an orthographic camera, however you're still required to supply a z value. If your game is a 2d game and everything is at distance 0 and the camera is not rotated then z doesn't matter. You would implicitly pass "0" as z value and get back 0 as z position.

  • 1
  • 2
  • ›

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

25 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

Related Questions

Game hangs on mouse rotation script 2 Answers

Move camera with mouse (2D) 1 Answer

Need help putting an object at a point based on screen coordinates 1 Answer

World point for MousePosition 1 Answer

Rotated Camera fix for Screen to World point overshoots 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