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

相关推荐
具身AGI36 分钟前
物理AI 人类学习路线:三大判断怎么兑现
大数据·人工智能·学习
Warson_L3 小时前
.NET 8 速成笔记 16:API 框架到底怎么用,谁是自带的、谁是自己写的、配置和测试库如何关联
c#·.net
我命由我123454 小时前
Excel - Excel 固定列,滚动时该列始终可见
运维·学习·职场和发展·excel·职场发展·运维开发·学习方法
minglie15 小时前
劳斯–赫尔维茨定理的证明
学习
学术小李6 小时前
用虚幻引擎(UE)打开Obj模型
游戏引擎·虚幻
淡海水6 小时前
01-10-认知篇-对比-方案选型矩阵
矩阵·c#·excel·游戏开发·luban
Nᴏsᴛᴀʟɢɪᴀ念6 小时前
Redis学习笔记|第一章:初识Redis与分布式架构
redis·笔记·学习
zhangrelay7 小时前
Arduino-MEGA-ESP单片机理论课笔记-12数字-13模拟-14智能工具-2026
笔记·单片机·嵌入式硬件·学习·机器人
张不无-Unity8 小时前
Unity的四种灯光Directional、Point、Spot 和 Area
unity·游戏引擎
ss2738 小时前
我用若依微服务写了套在线教育平台:8 个服务、313 个前端文件;若依微服务实战:课程、订单、学习进度全链路的在线教育后台- 学知在线教育云
前端·学习·微服务