Version: 2020.2

NavMeshAgent.SetDestination

切换到手册
public bool SetDestination (Vector3 target);

参数

target 要导航到的目标点。

返回

bool 如果目标请求成功,则返回 true,否则返回 false。

描述

设置或更新目标,从而触发新路径计算。

请注意,路径可能在几帧之后才可用。 计算路径时,pathPending 将为 true。 如果有效路径可用,代理将恢复移动。

using UnityEngine;
using UnityEngine.AI;

public class Example : MonoBehaviour { NavMeshAgent myNavMeshAgent; void Start() { myNavMeshAgent = GetComponent<NavMeshAgent>(); }

void Update() { if (Input.GetMouseButtonDown(0)) { SetDestinationToMousePosition(); } }

void SetDestinationToMousePosition() { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { myNavMeshAgent.SetDestination(hit.point); } } }