RaycastHit 没有检测到任何东西,尽管它明显击中了网格对撞机

问题描述 投票:0回答:0

我已经坚持了几个小时。我的运动代码使用光线投射,因为它是基于网格的。我开始使用 probuilder,似乎对撞机检测非常不可靠,大多数时候都没有检测到网格对撞机。

这是网格碰撞器和光线投射代码:

bool CanMove(Vector3 direction)
{
    if (direction.z != 0)
    {
        if (Physics.Raycast(zAxisOriginA, direction, out hit, rayLength))
        {Debug.Log(hit.collider);
            if (hit.collider != null)
            {
                Debug.Log(hit.collider);
                return false;
            }
        }             
        if (Physics.Raycast(zAxisOriginB, direction, out hit, rayLength)) 
        {
            Debug.Log(hit.collider);
            if (hit.collider != null)
            {
               
                return false;
            }
        }    
    }
    else if (direction.x != 0)
    {
        if (Physics.Raycast(xAxisOriginA, direction, out hit, rayLength))
        {Debug.Log(hit.collider);
            if (hit.collider != null)
            {
                Debug.Log(hit.collider);
                return false;
            }
        }    
        if (Physics.Raycast(xAxisOriginB, direction, out hit, rayLength))
        {Debug.Log(hit.collider);
            if (hit.collider != null)
            {
                Debug.Log(hit.collider);
                return false;
            }
        }    
    }
    return true;
}
unity3d raycasting mesh-collider
© www.soinside.com 2019 - 2024. All rights reserved.