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 Answer 1

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\$
1
  • \$\begingroup\$ Aha, so it is -z relative to the camera? \$\endgroup\$ Commented Mar 30, 2020 at 2:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.