我有一个简单的设置,包括一个地形、一个烘焙的导航网格和一个带有导航网格代理的胶囊,以及下面的脚本。应该发生的事情是用户点击地形的一个点,然后胶囊在障碍物周围导航。
在大多数情况下,一切似乎都正常,但是如果用户单击地形的平坦部分(而不是像树这样的不可行走区域),则光线投射不会命中,因此目的地不会更新(由调试日志“命中”)。我也希望能够点击可步行区域。
由于此代码在其他场景中运行良好,我怀疑这是代码的问题,并且可能是我的地形设置问题。
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);
}
}
}
}