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

相关推荐
笨鸟先飞的橘猫1 分钟前
mongo权威指南(第三版)学习笔记
笔记·学习
未来之窗软件服务3 分钟前
幽冥大陆(八十八 ) 操作系统应用封装技术C#自解压 —东方仙盟练气期
java·前端·c#·软件打包·仙盟创梦ide·东方仙盟·阿雪技术观
诺狞猫4 分钟前
黄山派播放TF卡MP4视频
学习·音视频·思澈·sifli
Noushiki4 分钟前
RabbitMQ 进阶 学习笔记2
笔记·学习·rabbitmq
Linux猿12 分钟前
2025年中国AI学习平板市场洞察白皮书 | 附PDF
人工智能·学习·电脑·研报精选
赵长辉2 小时前
AGI-rag学习:ChromaDB使用1,txt类型文档【20251016课复习】
python·学习·llm·agi
赵长辉2 小时前
AGI-rag学习:ChromaDB使用2,json类型文档,问答类内容,【20251016课复习】
学习·agi
小小星球之旅9 小时前
CompletableFuture学习
java·开发语言·学习
盐焗西兰花9 小时前
鸿蒙学习实战之路-ArkTS循环渲染_ForEach使用指南
学习·华为·harmonyos
巧克力味的桃子10 小时前
单链表 - 有序插入并输出学习笔记
笔记·学习