unity5之导航网格寻路系统-2使用NavMeshAgent实现类型英雄联盟右键行走功能

来源:互联网 发布:爱奇艺cyida软件源 编辑:程序博客网 时间:2024/04/17 02:37

代码都注释了,直接看代码。主要是使用NavMeshAgent这个组件的destination(获取或者尝试去在世界空间单位中设置代理的目的地。)这个属性来实现的。

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.AI;/// <summary>/// 当用户调用这个脚本的时候会自动为这个物体添加NavMeshAgent组件/// </summary>[RequireComponent(typeof(NavMeshAgent))]public class NPC : MonoBehaviour {    private NavMeshAgent Agent;    void Start () {        Agent = GetComponent<NavMeshAgent>();//获取导航组件    }    void Update () {        if(Input.GetMouseButtonDown(1))//右键按下        {            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从当前鼠标位置 获取射线的起点和方向             RaycastHit hit;//用来获取射线投射返回的信息            if (Physics.Raycast(ray,out hit,100))//Raycast投掷射线 碰撞到游戏物体就移动            {                Agent.destination = hit.point;//移动到这个点            }        }    }}
0 0
原创粉丝点击