Get mouse position in world space

I'm making a game using Unity and I have a little issue, I need to know the mouse position in world space, for that I try to set a GameObject at the mouse position using this code :

    Vector3 p = Input.mousePosition;
    Vector3 pos = Camera.main.ScreenToWorldPoint( p);
    testGameObject.transform.position = pos;

It works like a charm in Editor but in exe / apk, the GameObject dosn't follow the mouse:

Example 1

Example 2

The GameObject that is supposed to follow the mouse is the "1" inside a circle

1 answer

  • answered 2017-11-13 04:27 endrik exe

    If it's works in editor like a charm, then it should in build to.

    I see it already working in build, maybe what you want is to exactly place yor object to match the screen click point, but your object is far too close to the camera so that you can't see it.

    maybe there's a problem with the depth of the position from the camera. try adding something like

    Vector3 p = Input.mousePosition;
    p.z = 20;
    Vector3 pos = Camera.main.ScreenToWorldPoint(p);
    testGameObject.transform.position = pos;

    To add depth to the mouse position. try to change 20 to -20 if it's still doesn't work