Unity中的RaycastHit如何使用

RaycastHit用于从射线投射获取信息的结构,常与Physics.Raycast,Physics.RaycastAll配合使用。

------Unity中Physics.Raycast 的使用

    Ray ray;
    RaycastHit hit;
    void Start()
    {
        ray = new Ray(transform.position, transform.forward);
        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log(hit.collider);//命中的 Collider。
            Debug.Log(hit.point);//世界空间中射线命中碰撞体的撞击点。
            Debug.Log(hit.distance);//从射线原点到撞击点的距离。
            Debug.Log(hit.rigidbody);//命中的碰撞体的 Rigidbody。如果该碰撞体未附加到刚体,则值为 /null/。
        }
    }

当射线与物体发生碰撞时会做输出

 官方文档中给出了一下使用方法▼

猜你喜欢

转载自blog.csdn.net/cxy15978/article/details/130154131