我有一个简单的设置,其中包含一个地形,一个烤的导航网以及一个带有导航网代理的胶囊,以及下面的脚本。应该发生的事情是用户单击地形的某个点,然后胶囊在障碍物周围导航。
在大多数情况下,一切似乎都可以正常工作,但是如果用户单击地形的平坦部分(而不是像树这样的不可行走的区域),则射线投射不会命中,因此目标不会更新(通过调试日志“ hit”查看)。我也想点击可步行区域。
由于此代码在其他场景中也能正常工作,所以我怀疑这与代码有关,并且可能与我的地形设置有关。
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);
}
}
}
}
答案 0 :(得分:0)
相机距离地形太远。 线 如果(Physics.Raycast(ray,out hit,100f)) 指定最大距离为100f,因此它将仅达到网格的最高点。解决方案是将相机移近些,或增加该数量。