unity导航寻路代码demo

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class playerControl : MonoBehaviour
{
    private NavMeshAgent agent;
     
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //获取射线点击位置
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit))
            {
                //点击位置
                Vector3 point = hit.point;
                //设置该位置为导航目标点
                agent.SetDestination(point);
            }
        }
    }
}

你可能感兴趣的:(unity,unity,游戏引擎)