i need to get the mouse position on the screen, all methods i have tried so far do not work, i use debug log to print the position and it always is changing when i keep my mouse still and wehen i move it it still changes but no where near my mouse pinter, like the mouse position is slowly increasing, and some other methods make the mouse position to always be 0, i am using screen to world point, i have tried both the new and old input systems. nothing works, also this is in 3d and the camera in my game is constantly moving. worldPosition = cam.ScreenToWorldPoint(Mouse.current.position.ReadValue()) (this is in a function that is called from update) this was my closest attempt, nothing else worked, and i cant use raycast because there is no meshes or colliders behind the mouse also i believe that wouldnt be optimized. i have tried for hours getting this to work
Does your camera position or orientation change? Because what you display is not the mouse position but the projected mouse position since you use ScreenToWorldPoint. Furthermore unless you use an orthographic camera, a perspective camera needs a “z” value greater than 0 when passing a screen point to ScreenToWorldPoint, otherwise what you get back is just the camera position. All points on the screen with a distance of 0 when using a perspective camera will converge at the camera origin due to perspective.
So it’s not really clear what issue you actually have here. When your camera moves, so does the projected mouse position. The actual mouse position is just a 2d coordinate.
Expanding on what Bunny writes above… ScreenToWorldPoint meaning of Z:
Can I add an orthographic camera as an overlay?
What do you mean by overlay? Is it a 3d scene? Because if it is using an additional orthographic camera just for your ScreenToWorldPoint would not work since it would have no relation to what the perspective camera has rendered.
Though it’s not really clear what issue you want to solve. If your camera moves the resulting position would still change. That was your actually complaint, wasn’t it? What exactly do you want to do with that mouse position? In a 3d scene you usually want to do a Raycast to actually find the world space position under the cursor.
If you just need a world space direction, you can use ScreenPointToRay. This ray can by used in a Physics.Raycast call or if you just need the direction, you can grab the direction vector from the Ray.
