   Picking with XNA
  I'm trying to convert screen coords into 3D coords. I've been looking at some tutorials but can't seem to get it right. From trial and error, I know that 2D coords (0, 0) equals 3D coords (340, 165, Z). Whenever I unproject using (0, 0, Z) I get a vector3 very close to (0, 0, Z) back instead of something like (340, 165, Z). My code is below.  Viewport viewport = new Viewport();  viewport.X = 0;  viewport.Y = 0;  viewport.Height = Game1.BackBufferHeight;  viewport.Width = Game1.BackBufferWidth;  viewport.MinDepth = 1;  viewport.MaxDepth = 1500;  Matrix _cameraViewMatrix = Matrix.CreateLookAt( new Vector3(0, 0, -540), new Vector3(0, 0, 0), Vector3.Up);  Matrix  _cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), //how wide the camera sees viewport.AspectRatio, 1.0f, //near clipping plane 10000.0f); //far clipping plane  Vector3 nearSource = viewport.Unproject(new Vector3(0,0,-50), _cameraProjectionMatrix, _cameraViewMatrix, Matrix.Identity);What am I missing that is giving me the wrong results?   
  It seems like the near point isn't enough. I've been trying another approach, but still with no luck.  Viewport viewport = new Viewport();  viewport.X = 0;  viewport.Y = 0;  viewport.Height = Game1.BackBufferHeight;  viewport.Width = Game1.BackBufferWidth;  viewport.MinDepth = 1;  viewport.MaxDepth = 1500;  Matrix _cameraViewMatrix = Matrix.CreateLookAt( new Vector3(0, 0, -540), new Vector3(0, 0, 0), Vector3.Up);  Matrix  _cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), //how wide the camera sees viewport.AspectRatio, .01f, //near clipping plane 10000.0f); //far clipping plane  //pos holds 2D screen coords  pos.Z = 0;  Vector3 nearSource = Session.ScreenManager.GraphicsDevice.Viewport.Unproject(pos, _cameraProjectionMatrix, _cameraViewMatrix, Matrix.Identity);  pos.Z = 1;  Vector3 farSource = Session.ScreenManager.GraphicsDevice.Viewport.Unproject(pos, _cameraProjectionMatrix, _cameraViewMatrix, Matrix.Identity);  float x, y, t;  t = (0 - nearSource.Z) / (farSource.Z - nearSource.Z);  x = nearSource.X + (farSource.X - nearSource.X) * t;  y = nearSource.Y + (farSource.Y - nearSource.Y) * t;The 3D space x and y are still way off.   ;  This code works for me:D3DXVECTOR3 MousePos(int x, int y){D3DXMATRIX pm, vm, wm;D3DVIEWPORT9 vp;device->GetTransform(D3DTS_VIEW, &wm);device->GetTransform(D3DTS_VIEW, &vm);device->GetTransform(D3DTS_PROJECTION, &pm);D3DXMatrixIdentity(&wm);device->GetViewport(&vp);D3DXVECTOR3 p1;D3DXVec3Unproject(&p1, &D3DXVECTOR3((float)x, (float)y, (float)vp.MinZ), &vp, &pm, &vm, &wm);D3DXVECTOR3 p2;D3DXVec3Unproject(&p2, &D3DXVECTOR3((float)x, (float)y, (float)vp.MaxZ), &vp, &pm, &vm, &wm);   float mouse_y = 0.0f;float mouse_x = ((mouse_y-p1.y)*(p2.x-p1.x))/(p2.y-p1.y) + p1.x;float mouse_z = ((mouse_y-p1.y)*(p2.z-p1.z))/(p2.y-p1.y) + p1.z;   return D3DXVECTOR3(mouse_x, mouse_y, mouse_z);};  In the second example you made your near clip even closer to the camera. You should do the opposite, and try making 10 or more. You may be getting inaccuracy because the ray being cast is very short compared to the distance of objects.   