NavMeshAgent.SetDestination

设置或更新目的地

定义

public bool SetDestination (Vector3 target);
参数名类型必要说明
targetVector3目的地坐标

示例

using UnityEngine;
using UnityEngine.AI;

public class ExampleScript : MonoBehaviour
{
    public NavMeshAgent myNavMeshAgent;
    public Camera myCamera;

    private void Start()
    {
        myNavMeshAgent = GetComponent<NavMeshAgent>();
    }

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

    private void SetDestinationToMousePosition()
    {
        var ray = myCamera.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out var hit))
        {
            myNavMeshAgent.SetDestination(hit.point);
        }
    }
}