Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

ScreenToWorldPoint Problem

Discussion in 'Scripting' started by bobber205, Jul 31, 2009.

  1. bobber205

    bobber205

    Joined:
    May 12, 2009
    Posts:
    139
    For some reason, and I can't figure out why, the first vector printed from this statement, the one using ScreenToWorldPoint, is always (-92, 46.8, 69.8). Shouldn't it change based on mouse position?

    Thanks!

    Code (csharp):
    1.         Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition).ToString() + " --- " +  Input.mousePosition.ToString() );
    2.  
     
  2. Asse83

    Asse83

    Joined:
    Aug 1, 2009
    Posts:
    169
    Crazy-dog-CHAO and Shippety like this.
  3. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    As the above poster mentioned, it wants a vector3 as an argument with the z parameter being the distance from the camera in world units. Something like the following is what you're after.

    Code (csharp):
    1.  
    2.  
    3. // Forum code warning
    4.  
    5.    var zdepth : float;
    6.    var newVector : vector3;
    7.  
    8. function myfunction () {
    9.  
    10. // Retrieve mouse position
    11.  
    12.    var x = Input.mousePosition.x;
    13.    var y = Input.mousePosition.y;
    14.  
    15.  
    16. newVector = Camera.main.ScreenToWorldPoint (Vector3 (x, y, zdepth));
    17.  
    18. }
    19.  
    20.  
     
    dionnecm likes this.
  4. iceshaft07

    iceshaft07

    Joined:
    Jul 23, 2010
    Posts:
    293
    I am having the same problem. I am using Unity iPhone.

    Debug shows that the mouse input is changing, and the viewport point is changing, but not the world point.

    Code (csharp):
    1.  
    2. var input : Vector3 = Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 0);
    3. var testViewportPoint : Vector3 = Camera.current.ScreenToViewportPoint(input);
    4. var testWorldPoint : Vector3 = Camera.current.ViewportToWorldPoint(testViewportPoint);
    5. testWorldPoint.z = 0.0;
    6.  
    7.         Debug.DrawLine(objectToMove.transform.position,testWorldPoint);
    8.  
    9. var layerMask : int = ~(1 << 8);
    10. var hit : RaycastHit;
    11. if (!Physics.Linecast(objectToMove.transform.position,testWorldPoint,hit,layerMask))
    12. {
    13.     Debug.Log(layerMask + " (" + objectToMove.transform.position + ") (" + input + ") (" + testViewportPoint + ") (" + testWorldPoint + ")");
    14.     objectToMove.position = testWorldPoint;
    15. }
    16. else
    17. {
    18.     Debug.Log(layerMask + " (" + objectToMove.transform.position + ") (" + input + ") (" + testViewportPoint + ") (" + testWorldPoint + ") "+ hit.collider.gameObject.name);
    19. }
    20.  
    Sample Debug:
    Code (csharp):
    1.  
    2. -257 ((-191.5, -285.6, 0.0)) ((2.0, 2.0, 0.0)) ((0.0, 0.0, 0.0)) ((-191.5, -285.6, 0.0))
    3. -257 ((-191.5, -285.6, 0.0)) ((150.0, 249.0, 0.0)) ((0.5, 0.5, 0.0)) ((-191.5, -285.6, 0.0))
    4. -257 ((-191.5, -285.6, 0.0)) ((322.0, 458.0, 0.0)) ((1.0, 1.0, 0.0)) ((-191.5, -285.6, 0.0))
    5.  
    Anyone else having this problem?

    ScreenPointToRay seems to work okay.
     
  5. iceshaft07

    iceshaft07

    Joined:
    Jul 23, 2010
    Posts:
    293
    I should have mentioned that I am intentionally using the mouse position rather than iPhoneTouches as it makes the game more portable should I decide to transfer my code to another platform (such as Windows or Macintosh, or other future platform).
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You should use Camera.main rather than Camera.current. Camera.current is only appropriate for a few uncommon tasks.
     
  7. iceshaft07

    iceshaft07

    Joined:
    Jul 23, 2010
    Posts:
    293
    I can certainly try it, I don't think that is the problem (I only have 1 camera in my scene, so current should be equal to main, right?)
     
    fedorsky likes this.
  8. Slem

    Slem

    Joined:
    Jan 28, 2009
    Posts:
    191
    Camera.current can refer to the sceneview camera.
     
  9. iceshaft07

    iceshaft07

    Joined:
    Jul 23, 2010
    Posts:
    293
    Slem- I have confirmed that Camera.current instead of Camera.main was the problem. Thank you for your assistance.
     
  10. joefraga

    joefraga

    Joined:
    May 17, 2011
    Posts:
    8
    Great posts guys. Thanks, really helped a lot. It's true that the function to convert from screen point to 3d world point expects a Vector3.
    I my case i used Camera.nearClip to get the first plane the camera is drawing to draw some objects instead of GUI textures for my controllers.
    Thanks
     
  11. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,702
    **Edit.
    If you notive I left out Vector3.
    D'oh!

    I am having trouble with this.


    Code (csharp):
    1.  
    2. cPt = Camera.main.ScreenToWorldPoint(aPt.x, aPt.y, camDepth);
    3.  
    is returning the error... that this method does not support (float, float, float).

    :?
     
    Last edited: Feb 10, 2012
  12. kUr4m4

    kUr4m4

    Joined:
    May 14, 2013
    Posts:
    7
    what you are looking for is: cPt = Camera.main.ScreenToWorldPoint(new Vector3(aPt.x, aPt.y, camDepth));
     
  13. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    341
    *Shameless necro*. Thanks for solutions guys. It always returns the same point when you pass mouse position directly, you should give it a Z value other than 0.
     
  14. Shippety

    Shippety

    Joined:
    May 9, 2014
    Posts:
    31
    Hey, that helped me a lot! Thankya!