- Camera. Screenpointtoray (input.mouseposition), since Camera.tranform.position Input.mouseposition fired a ray and returned to Ray
- Camera. Screentoworldpoint (Input.mouseposition+new Vector3 (0, 0, (XXX))), converts screen coordinates to world coordinates, but must assign Z-values, which should be the projection of the camera to the plane of the point
- Do not forget distance parameters, using Layermask may be confused with distance
1Ray Ray =Camera.main.ScreenPointToRay (input.mouseposition); 2 Raycasthit hit;3 4 if(Physics.raycast (Ray, outhit,layermask))5 {6Vector3 dir = hit.point-camera.main.transform.position;//dir is a vector of points to which the camera points to a ray7Vector3 forward = Camera.main.transform.forward;//unit vector pointing directly ahead of the camera8Debug.Log ("Camera.main.ScreenToWorldPoint (input.mouseposition)" 9+Camera.main.ScreenToWorldPoint (input.mouseposition)Ten+NewVector3 (0,0, (Vector3.dot (dir, Forward)));//Vector3.dot (dir, forward) is the point multiplication of dir and forward, and the geometric meaning is the projection of Dir on the forward. OneDebug.Log ("Hit.point"+hit.point); A}
View Code
Camera. Screenpointtoray (), camera. Screentoworldpoint () and Raycast ()