0

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
    Your main problem is understanding the difference between 2D and 3D. The mouse cursor is moving on a 2D-plane (the screen plane) and therefore has no actual position in the scene (in fact, it has a 3rd coordinate limiting it to the screen plane). If you now shoot a ray from the (imagined) point of the spectator's eye through the location of the mouse cursor on the screen plane, you get a beam, not a single point. Now you need to calculate the intersections of that beam with every object in your scene and take that object which is nearest to the screen. This involves analytic geometry. – Psi Nov 12 '17 at 19:53
  • @Psi In fairness I don't actually know, but i'd be surprised if `Camera.main.ScreenToWorldPoint( p);` doesn't fire a ray and return its first hitpoint. – George Nov 12 '17 at 19:56
  • @George AFAIK, it returns the 3D-position of the mouse cursor as where it would be located in the scene if it was a real object. However, this is the 3d-position on the screen plane. When you want to know where a mouse cursor points at, you correctly must say that the cursor points to all points along a specific line (the beam between the eye and the cursor, extended into infinity). – Psi Nov 12 '17 at 20:00
  • Thanks for answers but i still don't understand. My camera is on Orthographic mode, not perspective, so i don't understand why it' not just a "point" in my point of view. Anyway i tried with a raycasthit2D : ` RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if(hit.collider != null && hit.collider.gameObject.tag == "touchZone") { testGameObject.transform.position = new Vector3(hit.point.x,hit.point.y, testGameObject.transform.position.z); }` But i still get exactly the same issue.. – Julien Renaud Le Coq Nov 13 '17 at 08:18
  • Well i'm new on Stackoverflow and i don't really understand how to put code in comment, it say `code` but when i put code into it, it change nothing.. – Julien Renaud Le Coq Nov 13 '17 at 08:23

2 Answers2

5

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

endrik exe
  • 606
  • 3
  • 10
  • I already tried to change the depth from -100 to 100, it change nothing exept the visibility of the object. X and Y pos are always the same... – Julien Renaud Le Coq Nov 13 '17 at 08:26
  • Do you have multiple camera in your scene? and where do you place your circle object – endrik exe Nov 13 '17 at 08:38
  • I have only 1 camera in my scene, the circle object is child of another GameObject witch is not child of any other GameObject. – Julien Renaud Le Coq Nov 13 '17 at 10:05
  • Well, thanks to you, i searched any other camera and there was one, the camera from scene below had a "don"t destroy on load" property, i fixed it, you helped a lot with your comment about camera, thanks you ! – Julien Renaud Le Coq Nov 14 '17 at 18:29
0

while this questions is quite old I've typed this up and it seems to work.

this might be a misconception on the way ScreenToWorldPos works,

it gives you a position from the x, y coords on the 2d axis of the screen then uses the z as a 3D depth finder essentially giving out a seemingly random position.

solution; try creating a ray and assign to it using ScreenPointToRay(p) then casting that ray, it should give the intended result in 3D as it is just a regular ray cast using the mouse cursor instead of just trying to find a world position with a vector.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 02 '23 at 09:44