Version: 2017.3

NavMeshAgent.SetDestination

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

参数

target 要导航到的目标点。

返回

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

描述

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

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

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { private AI.NavMeshAgent agent; void Start() { agent = GetComponent<AI.NavMeshAgent>(); } void Update() { RaycastHit hit; if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) agent.SetDestination(hit.point); } } }