Converting Mouse Position to World. Stationary Camera.

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?

1 Like

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.

I wanted to test this out but all code doesn’t have “hit” defined. Please help.

This question is old but, in case anyone needs it, there’s a way to do this without ray casts.

The last property gives the mouse position projected onto the transform.

    private static Vector3 cursorWorldPosOnNCP {
        get {
            return Camera.main.ScreenToWorldPoint(
                new Vector3(Input.mousePosition.x, 
                Input.mousePosition.y, 
                Camera.main.nearClipPlane));
        }
    }

    private static Vector3 cameraToCursor {
        get {
            return cursorWorldPosOnNCP - Camera.main.transform.position;
        }
    }

    private Vector3 cursorOnTransform {
        get {
            Vector3 camToTrans = transform.position - Camera.main.transform.position;
            return Camera.main.transform.position + 
                cameraToCursor * 
                (Vector3.Dot(Camera.main.transform.forward, camToTrans) / Vector3.Dot(Camera.main.transform.forward, cameraToCursor));
        }
    }

public Camera Cam;

public Camera cam1;

public RaycastHit RayHit;

public Ray ray;

public GameObject ObjectHit;

public Vector3 Hitpoint = Vector3.zero;

update ()

{

            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.