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.编写代码

相关推荐
zcc8580797624 分钟前
Unity 自动生成UI绑定+MVVM 架构模板
unity
一條狗22 分钟前
学习日报 20260423|Vue SPA 部署到 Spring Boot:404/500 错误排查与解决方案1
vue.js·spring boot·学习
炽烈小老头23 分钟前
【每天学习一点算法 2026/04/23】盛最多水的容器
学习·算法
火星papa38 分钟前
C# 【通过NPIO读写Excel表】
c#·excel·npoi
寒秋花开曾相惜41 分钟前
(学习笔记)4.1 Y86-64指令集体系结构(4.1.6 一些Y86-64指令 )
linux·运维·服务器·开发语言·笔记·学习·安全
蓝桉~MLGT1 小时前
Ai-Agent学习历程—— Harness和Memory介绍和应用 & vibe Coding工具选择
人工智能·学习
GHL2842710901 小时前
LangChain学习
学习·ai·langchain
知识分享小能手1 小时前
ECharts入门学习教程,从入门到精通,综合实战——ECharts电商数据可视化完整知识点+案例代码(8)
学习·信息可视化·echarts
噜噜噜阿鲁~1 小时前
python学习笔记 | 7.2、高级特性-迭代
笔记·python·学习
LF男男2 小时前
MK - Grand Mahjong Game-
unity·c#