- Home /
ScreenToWorldPoint returning weird value.
Hi, I am trying to print the clicked point in global point. But it always returning the same value, no matter where I click: X: 5.828779, Y: 0. I don't have idea what to try. I need this values to do some stuff, and I lost my day trying. Thanks for any help.
void Update () {
if (Input.GetMouseButtonDown(0)) {
Vector3 cameraVector = Camera.main.ScreenToWorldPoint(Input.mousePosition);
print("Posição X: " + cameraVector.x);
print("Posição Y: " + cameraVector.y);
}
Answer by Eric5h5 · Jan 02, 2013 at 01:14 AM
It's actually not weird, it's expected, since you haven't given it any depth value, so it's always going to return the camera position. You need something greater than 0 for the z element of the Vector3 in ScreenToWorldPoint.
Right, that means since you passed a Vector2 variable to a Vector3 parameter, Unity will implicit convert the Vector2 into a Vector3 by setting the missing 3rd component to 0.
So your mouse position (x,y) becomes (x,y,0). Like you can read in the docs the z value deter$$anonymous$$es the distance from the camera.
Actually Input.mousePosition returns a Vector3, with z=0. Although that's certainly true about the Vector2 implicit conversion (and that's what would happen if you used Event.current.mousePosition, which does return a Vector2).
It worked, I put for example z=2 and he gave me the points when clicked in update. But I don't know why it does not work with z=0, and give me the same values no matter where I click because z=0 2D space has global values too. Is this means that if I want to know point in z=0, it will not work?
Actual code:
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
Vector3 vetor3 = new Vector3(Input.mousePosition.x,Input.mousePosition.y,-1);
Vector3 cameraVector = Camera.main.ScreenToWorldPoint(vetor3);
print("Posição X: " + cameraVector.x);
print("Posição Y: " + cameraVector.y);
}
}
As I mentioned, 0 = camera position. If the distance you're trying to get is 0 units away from the camera, it doesn't matter where the mouse position is, it's mathematically impossible for it to be anything other than the camera position. You need to provide at least a tiny bit of depth.
Answer by UsefulYdyot · May 17, 2020 at 09:47 PM
For 2D at least you can pass negative camera z position as z. This is what worked for me.
cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -cam.transform.position.z));
Took me long to understand this. Thanks for the hints in the answers of this question.
my 2cents to complete the 2D:
Vector3 mouseWzPos = new Vector3(Input.mousePosition.x,
Input.mousePosition.y, Camera.main.transform.position.z);
Vector3 mousePos = Camera.main.ScreenToWorldPoint(mouseWzPos);
mousePos.z = 0;
Your answer
Follow this Question
Related Questions
Scale Z Axis Movement According to Camera Tilt (Drag Control Camera) 0 Answers
Controlling object movement with mouse 0 Answers
Input.mousePosition only detects motion outside of window if I move really slow. 1 Answer
Zoom to center of Pinch Gesture 1 Answer
my script not working to move plane to mouse position 1 Answer