0
\$\begingroup\$

I'm learning a course on unity and when we started learning about taking mouse input the following code is used.

Vector3 pos = Input.mousePosition;
pos.z = -Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
gameObject.transform.position = pos;

My problem is in the second line. Shouldn't our Z be 0 instead of minus wherever our camera is? Because the plane where we our designing our game is in z = 0, right?

Please go slow on me I'm still new to unity. Thank you!

\$\endgroup\$
1
\$\begingroup\$

The z-coordinate given to ScreenToWorldPoint is the distance from the camera plane. If you set z to 0, the position returned will be in the camera's plane.

Is the ground plane at (0,0,0)? And the camera is at (0,0,-z) and pointing along the +z direction?

\$\endgroup\$
  • \$\begingroup\$ Aha, so it is -z relative to the camera? \$\endgroup\$ – Mahmood Darwish Mar 30 '20 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.