栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity Navigation

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity Navigation

自动寻路的配置
  1. 为路上的地面和障碍物勾选好好static

  2. 在导航面板出烘焙

  3. 玩家身上挂载代理组件

  4. 烘焙完成

其中Navigation static是一般障碍物的,下面那个Off Mesh Link Generation是可跳跃的地面勾选的这样才能在可跳跃面板搜到他。

部分代码实现

玩家类挂在玩家身上

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

public class Player : MonoBehaviour
{
    private NavMeshAgent agent;
    private LineRenderer lineRender;
    void Start()
    {
        agent = transform.GetComponent();
        lineRender = transform.GetComponent();
        if (agent==null)
        {
            Debug.LogError("未找到代理!!!");
        }
    }
    private void Update()
    {
        DrawLine();
    }
    public void MoveToTarget(Vector3 tar)
    {
        agent.SetDestination(tar);
    }
    public void DrawLine()
    {
        if (lineRender!=null)//在外面给该物体身上挂载一个LineRender组件
        {
            lineRender.positionCount = agent.path.corners.Length;
            lineRender.SetPositions(agent.path.corners);
        }
    }
}

挂在其他物体上控制玩家的代码

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

public class PlayerController : MonoBehaviour
{
    public Player currentAgent;
    public RaycastHit hit;
    
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit,1000,1<<8))
            {
                if (currentAgent!=null)
                {
                    currentAgent.MoveToTarget(hit.point);
                }
            }
        }
    }
}

此处只做出了移动,跳跃和爬楼梯可以去B站看视频

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901455.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号