Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Vector3 = ScreenToWorldPoint(Vector3) ?

Discussion in '2D' started by AreebM, Aug 3, 2014.

  1. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    I'm taking the pixel location of a GUITexture and trying to move a Sprite to the same location.

    Here's my code, getting an example pixel location of (250, 65, 5)

    Code (CSharp):
    1. Vector3 screenPoint = new Vector3(250, 65, 5);
    2.  
    3. void Start () {
    4. Vector3 worldPos = camera.ScreenToWorldPoint(screenPoint);
    5. transform.position = new Vector3(worldPos);
    6. }
    The error I'm getting is:
    "The type `UnityEngine.Vector3' does not contain a constructor that takes `1' arguments"
    for
    "transform.position = new Vector3(worldPos)"

    I don't really know what is going on between "Screen" and "World" and what exactly is being converted. Also not sure what the third coordinate "5" is going to do.

    If I understood these things, I'd probably know how to use the function ScreenToWorldPoint, so I'd appreciate an explanation. Thank you.
     
    Last edited: Aug 3, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you want to get the world position, the screen position is not enough. The depth is also needed. The 5 means, it is at a depth of 5 units. This point (250, 65) with a depth of 5 is then translated into world coordinates.
     
  3. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    Of course, that makes sense. But in the Screen coordinates, what would I put instead of 5 if I defined the position of the GUITexture to be Rect( "x pixel position", "y pixel position", "scale on x", "scale on y") - meaning I didn't define a "z pixel position"
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Just pick any value for z (maybe 0 doesn't work, then you could try 1) if you are not sure what makes sense. You want to get world coordinates, that's why you have to provide the depth information. The depth depends on the application. If the object you want to position still has to stay on the ground, it would be the depth of the ground at that coordinate.
     
  5. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    229
    Just pick any value for z (maybe 0 doesn't work, then you could try 1) if you are not sure what makes sense

    no you'll need the correct depth value (unless its an orthogonal view)
    Im not sure if unity has a readdepth function, if not you could calculate this yourself

    another way is go
    p1 = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
    p2 = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane));
    cursorposWS = MathFunctionsCS.return_intersection_point_of_PLANE_LINESEGMENT(plane, p1, p2);

    plane = a plane constructed with the normal facing the camera, and a vertex any where on that plane, the z value == the depth of the object you want to move
     
  6. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    I'm still stuck on getting this to work. To reiterate, I've successfully placed my GUI Texture, which should now be at the same location on every device (left-center of the screen). Now I need a Sprite to instatiate at the same position as the GUI Texture.

    I have the above script attached to my GUI Texture and it tells me that there is no camera attached to the texture so the game won't run.

    Forget about ScreenToWorldPoint - I just don't understand it right now, and it isn't the crux of my issue.

    Can someone give me a better solution?
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Post the complete script.
     
  8. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    Create an GameObject and put it as a child of the GUI Texture Game Object. Then on the GameObject you created add a Sprite Renderer component.
    In your script get this Game Object, by assigning it to a variable or looking for it with "FindGameObject..." functions, and then set the Sprite you want to its sprite renderer.