So I’m just trying to get my mouse position into a world position. I’ve seen a thousand answers where you Camera.main.ScreenToWorld(Input.MousePosition) it doesnt work. the value it returns is the same (the cameas position 10.5,25,10.5)
This is an overhead perspective not first person thing. So how do I get a mouse pos to become a world one?
Camera.main.ScreenToWorldPoint(Input.mousePosition) will not work. The issue that you are mapping a 2D coordinate to a 3D world. To make it work, you need to specify the distance in front of the camera as the ‘Z’ coordinate in the position you want to map. So for example if you wanted the position 10 units in front of the camera:
var v3 = Input.mousePosition;
v3.z = 10.0;
v3 = Camera.main.ScreenToWorldPoint(v3);
You say ‘overhead’ perspective. As long as you mean straight down, setting the ‘z’ parameter is easy. If your camera is at an angle with respect to the “ground,” then you want to a different approach to map mouse position to world space.
Cam = Camera.allCameras;
cam1 = Cam[0];
if (cam1 != null)
{
//Cast Ray from Camera through Mouse
ray = cam1.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RayHit))
{
ObjectHit = RayHit.transform.gameObject;
Hitpoint = RayHit.point;
if (ObjectHit != null)
Debug.DrawLine(cam1.transform.position, Hitpoint, Color.blue, 0.5f);
}
//Cast Ray from Camera through Mouse Ends
}
}
Hitpoint is your new World Coridinates of the mouse pointer. this casts a line from the camera through the mouse pointer. Rember tochange camera to your camera, this is straight from my script Enjoy Exvalid PS YOUR RECAPTCHA-VERIFICATION SYSTEM IS THE WORST THING IVE EVER HAD TO ENDURE IN MY LIFE TO MAKE THIS POST AND EDIT IT REMOVE IT AT ONCE ITS TERRIBLE FIRE WHOEVER MADE IT.