Unity射线检测

2019-05-29 16:01:38 浏览数 (1)

Physics.RayCast方法发射射线,射线碰撞的信息存在RaycastHit中。

代码语言:javascript复制
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit raycastHit;
        Physics.Raycast(ray,out raycastHit, 1000f);
        navMeshAgent.SetDestination(raycastHit.point);

Camera.main.ScreenPointToRay

Returns a ray going from camera through a screen point. Resulting ray is in world space, starting on the near plane of the camera and going through position's (x,y) pixel coordinates on the screen (position.z is ignored).

Input.mousePosition

The current mouse position in pixel coordinates. (Read Only) The bottom-left of the screen or window is at (0, 0). The top-right of the screen or window is at (Screen.width, Screen.height).

Physics.RayCast:

bool True if the ray intersects with a Collider, otherwise false.

RaycastHit

Structure used to get information back from a raycast. 碰撞的信息存在此对象中

0 人点赞