Vector3 = ScreenToWorldPoint(Vector3) ?

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)

Vector3 screenPoint = new Vector3(250, 65, 5);

void Start () {
Vector3 worldPos = camera.ScreenToWorldPoint(screenPoint);
transform.position = new Vector3(worldPos);
}

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.

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.

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”

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.

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

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?

Post the complete script.

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.