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

相关推荐
roman_日积跬步-终至千里1 小时前
【强化学习基础(2)】被动强化学习:学习价值函数
学习
逢考必过@k2 小时前
6级550学习ing
学习
大侠课堂2 小时前
C#经典面试题100道
开发语言·c#
陈天伟教授3 小时前
基于学习的人工智能(7)机器学习基本框架
人工智能·学习
时光追逐者4 小时前
Visual Studio 2026 现已正式发布,更快、更智能!
ide·c#·.net·visual studio
Z***G4794 小时前
网络爬虫学习:借助DeepSeek完善爬虫软件,实现模拟鼠标右键点击,将链接另存为本地文件
爬虫·学习·计算机外设
周杰伦fans5 小时前
C# 正则表达式完全指南
mysql·正则表达式·c#
我命由我123456 小时前
微信开发者工具 - 模拟器分离窗口与关闭分离窗口
前端·javascript·学习·微信小程序·前端框架·html·js
DKPT6 小时前
ZGC和G1收集器相比哪个更好?
java·jvm·笔记·学习·spring
Main. 246 小时前
从0到1学习Qt -- 常见控件之显示类控件
qt·学习