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

相关推荐
青衫码上行38 分钟前
【Java Web学习 | 第七篇】JavaScript(1) 基础知识1
java·开发语言·前端·javascript·学习
fs哆哆2 小时前
PDFsharp库的加密功能-学习笔记
笔记·学习
qzhqbb6 小时前
神经网络—— 学习与感知器(细节补充)
神经网络·学习
风暴之零7 小时前
卡尔曼滤波学习
人工智能·学习·机器学习
2401_860319528 小时前
【无标题】
开发语言·学习·rust
Nebula_g9 小时前
C语言应用实例:解方程(二分查找)
c语言·开发语言·学习·算法·二分查找·基础
好望角雾眠9 小时前
第四阶段C#通讯开发-5:TCP
网络·笔记·网络协议·tcp/ip·c#
三品吉他手会点灯9 小时前
stm32f103学习笔记-16-RCC(第2节)-讲解系统时钟配置函数SetSysClockTo72()
笔记·stm32·单片机·嵌入式硬件·学习
InCerry9 小时前
.NET周刊【11月第1期 2025-11-02】
c#·.net周报·.net周刊
wdfk_prog10 小时前
[Linux]学习笔记系列 -- [kernel]cpu
linux·笔记·学习