1
\$\begingroup\$

I'm trying to convert the mouse position (Input.mousePosition) into world points, but for whatever reason the X and Y coordinates are always, always 0.

Here, I printed mousePosition and ScreenToWorldPoint()'s output:

Debug Logs

The -10 in the Z is understandable, but why do I get 0s in X and Y regardless of the actual, varying mouse position?

This is inside a custom display I set inside Game View (1440x3040). I tried Free Aspect, but that didn't change anything.

I tried parenting the script to an empty game object centered on origin just in case, but that didn't do anything either.

The code:

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log(mousePos);
Debug.Log(Input.mousePosition);

Or, more generally:

private bool hitProjectile() {
    bool hit = false;
    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    Debug.Log(mousePos);
    Debug.Log(Input.mousePosition);

    if ((mousePos.x >= (pt.position.x - radius) && mousePos.x <= (pt.position.x + radius)) &&
        (mousePos.y >= (pt.position.y - radius) && mousePos.y <= (pt.position.y + radius)))
        hit = true;

    return hit;
}

The game is in 2D, the camera is perspective, and I'm using URP. The script is trying to determine if the mouse clicked on a circle (/w a CircleCollider2D).

What the hell is going on? I hope this is not a URP thing. I've had nothing but bad experiences with it.

\$\endgroup\$
2
  • \$\begingroup\$ Also, don't forget to search for existing Q&A about your issue. \$\endgroup\$ – DMGregory Jun 20 '20 at 11:44
  • \$\begingroup\$ @DMGregory I did, and I did in fact check out the documentation — multiple times. I guess I glossed over the z position, because I don't remember ever having this problem before, despite having done it many, many times. Maybe I accounted for the camera offset beforehand, which I didn't do now because I just wanted to see that the code I'd written so far was working before proceeding; or maybe it was something else. Whatever the case, it works now, and that's what matters. \$\endgroup\$ – verified_tinker Jun 20 '20 at 11:51
1
\$\begingroup\$

Whenever you have trouble with an API method, your first step should be to check the documentation on that method:

Camera.ScreenToWorldPoint

public Vector3 ScreenToWorldPoint(Vector3 position);
public Vector3 ScreenToWorldPoint(Vector3 position, Camera.MonoOrStereoscopicEye eye);

Parameters

  • position A screen space position (often mouse x, y), plus a z position for depth (for example, a camera clipping plane).

  • eye By default, Camera.MonoOrStereoscopicEye.Mono. Can be set to Camera.MonoOrStereoscopicEye.Left or Camera.MonoOrStereoscopicEye.Right for use in stereoscopic rendering (e.g., for VR).

Returns

Vector3 The worldspace point created by converting the screen space point at the provided distance z from the camera plane.

Note the emphasis I've added on the z depth. Your code doesn't tell the camera what depth it wants the world point to be at. It just converts the 2D vector Input.mousePosition into a 3D vector implicitly. That implicit version puts a zero in the z coordinate. And of course if you take any screen position at zero depth from the camera, you get the camera's location itself. The frustum hasn't had any room to fan out.

Try something more like this:

Vector3 screenPosDepth = Input.mousePosition;
screenPosDepth.z = 5.0f; // Give it a depth. Maybe a raycast depth, maybe a clipping plane...
Vector3 mousePos = Camera.main.ScreenToWorldPoint(screenPosDepth);
\$\endgroup\$
0
1
\$\begingroup\$

Couple of suggestions here...

First off, try setting the mouse position's z to a positive value. As I recall it's to do with the near clipping plane (in the Camera settings). Set it to a value greater than that and it should work.

Secondly, take a look at OnMouseEnter(). It's called on a MonoBehaviour when the mouse cursor enters it. If you cache this value in a variable (and clear it on OnMouseExit()) you can just query the variable when the mouse button is down...

Danny

\$\endgroup\$

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