Unity 新导航寻路演示(2)

对于静态场景来说,只需要3步

1.为场景Ground添加网格表面组件并烘焙

2.为player添加导航代理

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerMove : MonoBehaviour
{
    private NavMeshAgent agent;
    private Animator animator;
    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0))
            { 
            Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit,1000))
                {
                agent.SetDestination(hit.point);
            }
        }
        Vector3 v =agent.velocity;
        if(v.magnitude >0.1)
            {
            animator.SetBool("isRunning",true);
        }
        if(v.magnitude < 0.1)
            {
            animator.SetBool("isRunning", false);
        }
    }
}

3.编写代码

相关推荐
Answer1st1 小时前
【嵌入式学习】嵌入式原理知识-定时器(六)
学习
\光辉岁月/2 小时前
1.mybatis学习-基础
学习·mybatis
地狱为王2 小时前
Unity使用NVIDIA的Audio2Face驱动口型
unity·游戏引擎
へ蟲児.2 小时前
2026三大自带完整体系的英语学习App深度测评:哪款更适合儿童启蒙?
学习
小羊没烦恼!2 小时前
在Scrum中实施敏捷建模
java·开发语言·windows·算法·c#
苦猿的大模型日记3 小时前
Day64|从0学习 Claude Code(十四):MCP,给 Agent 装个工具插座
学习
m4Rk_3 小时前
【论文阅读】Agent 记忆机制(82):ReasoningBank——从成功与失败经验中沉淀可复用的推理记忆
论文阅读·人工智能·学习·开源·github
Lost of 程序猿3 小时前
建造者模式实战:告别“十参数构造函数“的数据导出任务
后端·设计模式·c#·asp.net
ebiobiz4 小时前
STM32F4 Unity 单元测试与多次测试失败率统计详细步骤
stm32·unity·单元测试