3

Whenever I develop games that use mouse input, I will get confused of calculating the mouse position. Especially the z position.

The ways I saw many using.

  1. mouse position z = mouse position y.

  2. z = distance between camera and object.

  3. z = difference b/w object z and camera z. (I am using. Doesn't work when camera and object is rotated).

  4. z = some arbitrary value. (many use 0 and some other values).

  5. others.

Which method is correct? Is there any other method which is correct?

Please let me know.

Krishna Kumar
  • 187
  • 1
  • 17
  • It's not clear what you are asking given you don't seem satisfied with provided answers. Rigorously, the mouse position is not defined in 3d space. In case that's what you are after yes, you can map the rendered rectangle to 3d space, thus you can determine a non projective 3d coord for the mouse. Is this what you are looking for? –  Apr 18 '15 at 15:43

3 Answers3

1

The same answer as in your second mouse based question applies here too: Mouse based aiming Unity3d

TL;DR: use a raycast from camera to intersect the plane that the action is on.

Community
  • 1
  • 1
Krzysztof Bociurko
  • 4,575
  • 2
  • 26
  • 44
0

Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);

pz.z should be what you are asking for if I'm understanding right. Tell me if it works

Mario Garcia
  • 623
  • 5
  • 17
  • 1
    No, This I know. But this doesn't give correct mouse position in world coordinates. You're skipping z value.here. The code converts 2D screen coordinates to world coordinates. But this doen't consider z value. – Krishna Kumar Apr 17 '15 at 09:08
  • 2
    You are projecting the mouse, with 2D coords, in 3D. This does give correct mouse position. Based on your game logic, you're the only one to choose how you handle z. – Kamalen Apr 17 '15 at 09:25
  • What for do you want the z position now? Maybe we can help in some other way – Mario Garcia Apr 17 '15 at 09:34
0

The mouse position is in theory external to the game world itself. Therefore, the mouse position relates simply to X and Y co-ordinates of the screen space in which you are interacting with your game (IE width:height of your game).

What you're asking seems to be more of "How do I model the mouse position in my game world?"

As noted by Mario, Camera.main.ScreenToWorldPoint(Input.mousePosition) will convert your mouse position to world co-ordinates. However, this assumes that your main camera is where you want to convert to. In reality, you want to call the ScreenToWorldPoint method on the Camera that is rendering whatever space it is you are wanting to interact with. For example, you may have your main game world at (0, 0, 0) but you may be rendering your GUI on top using a separate camera that renders objects at (-5000, 0, 0).

To answer your question, to model the mouse z position it should simply be the same z value as your Camera. You can then perform calculations on that value to suit your particular needs.

IE:

1) mouse.position.z = mouse.position.y - These are entirely different. Now you're just using an arbitrary value 2) Distance between camera and object - That's a calculation made from your original object.position.z and original mouse.position.z. Not your actual z value. 3) See 2. 4) See 1.

Atra Viator
  • 535
  • 2
  • 6