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

相关推荐
蓝斯4975 小时前
[原创]《C#高级GDI+实战:从零开发一个流程图》第章:增加贝塞尔曲线,上、下、左、右连接点
java·c#·流程图
暗影凋落6 小时前
CMake构建学习笔记-SQLite库的构建
笔记·学习·sqlite
画面无声7 小时前
STM32的SPI通信原理与练习记录
c语言·stm32·单片机·嵌入式硬件·学习
你有哈莫吗8 小时前
CMake构建学习笔记-iconv库的构建
笔记·学习
ctlover9 小时前
Python学习第4日
学习
小弥儿10 小时前
GitHub今日热榜 | 2026-08-01:硬件安全工具与AI教育并行
人工智能·学习·开源·github
秋雨梧桐叶落莳10 小时前
计算器仿写总结
学习·macos·ios·objective-c·cocoa
统计学小王子11 小时前
《数学少年-从正负号到几何原本》(第一章:“刻度之上,零为分界“)--1.2 电梯与账单里的相反数字
学习·初中数学·初中数学科普·数学科普
流云鹤11 小时前
03Java学习day(3)
java·学习
我命由我1234512 小时前
棘轮效应极简理解
经验分享·学习·职场和发展·产品运营·求职招聘·职场发展·学习方法