0

我有一个简单的设置,包括一个地形、一个烘焙的导航网格和一个带有导航网格代理的胶囊,以及下面的脚本。应该发生的事情是用户点击地形的一个点,然后胶囊在障碍物周围导航。

在大多数情况下,一切似乎都正常,但是如果用户单击地形的平坦部分(而不是像树这样的不可行走区域),则光线投射不会命中,因此目的地不会更新(由调试日志“命中”)。我也希望能够点击可步行区域。

由于此代码在其他场景中运行良好,我怀疑这是代码的问题,并且可能是我的地形设置问题。

public class PlayerController : MonoBehaviour
{
NavMeshAgent navMeshAgent;
//public Transform target;
// Start is called before the first frame update
void Start()
{
    navMeshAgent = GetComponent<NavMeshAgent>();

}

// Update is called once per frame
void Update()
{
    //navMeshAgent.SetDestination(target.position);
    SetTargetByMouseClicking();

}

void SetTargetByMouseClicking()
{
    if (Input.GetMouseButtonDown(0))
    {

        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit,100f))
        {
            Debug.Log("HIT");
            navMeshAgent.SetDestination(hit.point);
        }

    }
}

}
4

1 回答 1

0

相机离地形太远了。if (Physics.Raycast(ray, out hit,100f)) 行指定最大距离为 100f,因此它只会到达网格的最高点。解决方案是将相机移近,或增加该数字。

于 2019-11-18T02:41:31.297 回答