其他分享
首页 > 其他分享> > Unity中射线的几种

Unity中射线的几种

作者:互联网

    //只能触发到一个Collider
    Ray ray_One = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray_One, out (hit)))
    {
        Debug.Log("得到物体" + hit.collider.gameObject.name);
    }
    //能触发到射线碰到的所有物体
    Ray ray_More = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit[] hitArray = Physics.RaycastAll(ray_More);
    for (int i = 0; i < hitArray.Length; i++)
    {
        Debug.Log("得到物体" + hitArray[i].collider.gameObject.name);
    }
    //以某个点发射球形射线,得到射线碰到的所有物体
    Collider[] ray_circle = Physics.OverlapSphere(transform.position, 3);
    if (ray_circle.Length > 0)
    {
        for (int i = 0; i < ray_circle.Length; i++)
        {
            Debug.Log("得到物体" + ray_circle[i].gameObject.name);
        }
    }

标签:hit,Log,物体,几种,射线,Unity,circle,hitArray,ray
来源: https://blog.csdn.net/qq_32688843/article/details/119320456