# [unity] screen coordinates to world coordinates

2020-12-07 23:16:14

1. Use ScreenToWorldPoint The acquired screen position is directly converted into world coordinates ：

pit 1： Get the screen coordinates of Input.mousePosition It's a 2d coordinate ,z The axis value is 0, This z The value is relative to the current camera Of , Zero means z The axis coincides with the camera , So give ScreenToWorlfdPoint The value , It can't be transmitted directly Input.mousePosition, Otherwise, the world coordinates obtained will always have only one value ;

pit 2： To solve the problem 1, So that the incoming z Axis value , So what's the value , You can take the current camera's z The absolute value of the axis value , It means the distance from the camera , The position of the generating point will end up in a plane （ Because of the incoming z The value is fixed ）, It is more suitable for taking points in a plane

2. Radiographic testing , Get screen coordinates , Emit radiation , Get collision information , More accurate , The X-ray hits the location with the collider , Just go back to this position

```Ray ray= mainCamera.ScreenPointToRay(screenPos);

RaycastHit hit;

bool ishit=Physics.Raycast(ray,out hit);

if (ishit)

{

Vector3 spacePos = hit.point;

print("screenPos:" + screenPos);

print("spacePos:" + spacePos);

}```

Link to the original text ：https://blog.csdn.net/qq_38806355/article/details/82938553

https://chowdera.com/2020/12/202012072315501585.html