Unity射线检测
例图:
步骤1. 实现发射射线代码
public LayerMask layerMask;
private Vector3 currentLookTarget = Vector3.zero;
void FixedUpdate(){
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//获取鼠标的位置
Debug.DrawRay(ray.origin, ray.direction * 1000, Color.green);//在游戏运行时,这将会在场景(Scene)视图中画一条射线。
if (Physics.Raycast(ray, out hit, 1000, layerMask, QueryTriggerInteraction.Ignore))
{
if (hit.point != currentLookTarget)
{
currentLookTarget = hit.point;
}
}
}