我已经坚持了几个小时。我的运动代码使用光线投射,因为它是基于网格的。我开始使用 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;
}