0
\$\begingroup\$

I'd like the game object to follow the mouse but I have trouble doing this when the object is in 3D and the camera has been rotated.

The only thing that seems to be true (and still it works only when camera isn't rotated) is :

  • The visible X for the camera is equal to 2 * Mathf.Abs(object.position.z - camera.position.z)

  • The visible Y for the camera is equal to VisibleX * (camera.pixelHeight / (float)camera.pixelWidth) (casting to float to avoid int divided by int returning an int)

\$\endgroup\$
  • \$\begingroup\$ It looks like you're using Unity, is that right? Be sure to describe how you want the object to follow the mouse. Since we're in 3D, there's an infinite set of points along the ray from the camera through the mouse position, so we need some rule to choose which one to use. Often we'll snap the object to a particular depth from the camera, or snap it onto a particular plane or mesh in your scene, depending on what you want to do in your game. \$\endgroup\$ – DMGregory Jan 23 at 15:26
  • \$\begingroup\$ Ah yeah sorry i'll add that i use Unity, what i want is to have the object following the mouse on the screen. @DMGregory \$\endgroup\$ – nalka Jan 23 at 15:32
  • \$\begingroup\$ Yes, but as I said for any position of the mouse on screen, there are infinite points in 3D space that would position the object at the mouse position on screen — just closer or further away in depth. To know which one of these to use, we need some second rule like "the object should follow the mouse AND stay at a depth of 10 units in front of the camera" or "the object should follow the mouse AND stay on my ground plane" etc. \$\endgroup\$ – DMGregory Jan 23 at 15:39
  • \$\begingroup\$ well for example if the camera is looking at the object with a 45 degrees angle from above, when you move the mouse up half of the mouse movement on y axis will be given to the object's y axis and the other half to the z axis, if that's clear \$\endgroup\$ – nalka Jan 23 at 15:46
  • \$\begingroup\$ So you want to keep the object in a plane of constant depth in front of the camera, it sounds like. \$\endgroup\$ – DMGregory Jan 23 at 15:58
0
\$\begingroup\$

If you want an object to snap to the mouse position while keeping the same depth in front of the camera, you can use the ScreenToWorldPoint convenience method:

// Capture the position of the mouse on screen/window,
// measured in pixels from the bottom-left corner.
Vector3 screenPosition = Input.mousePosition;

// Track the current depth of the object in front of the camera.
// (You could also set this to some fixed constant or stored variable)
screenPosition.z = Vector3.Dot(
                     object.transform.position - camera.transform.position,
                     camera.transform.forward
                   );

// Transform this screen position to a world position, 
// using the camera's transformation & projection/viewport settings.
Vector3 worldPosition = camera.ScreenToWorldPoint(screenPosition);

// Snap the object to this position.
object.transform.position = worldPosition;
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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