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

相关推荐
m4Rk_10 分钟前
【论文阅读】Agent 记忆机制(48):Fine-Mem——用细粒度奖励解决长期记忆管理中的奖励稀疏与信用分配
论文阅读·人工智能·学习·开源·github
是隼人12 分钟前
buuctf-pwn axb_2019_fmt32题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
zhchyun200819 分钟前
【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(05)
ui·unity·游戏引擎
txg66623 分钟前
Less Is More:如何用半监督学习提升漏洞检测能力
人工智能·深度学习·学习·安全·开源软件
FAREWELL0007531 分钟前
02-资源包形态-AB与Addressables
学习
小弥儿42 分钟前
GitHub今日热榜 | 2026-08-23:终端编码Agent与Skills生态扎堆
学习·开源·github
石头猫灯1 小时前
WordPress wp2shell漏洞断点逐过程到注入点
android·学习
会编程的吕洞宾1 小时前
DeepAgents In Action学习(Third)
python·学习·django
Be for thing1 小时前
【嵌入式成长10】STC89C51蜂鸣器|有源/无源蜂鸣器区分、三极管驱动、定时器播放音乐实战
单片机·嵌入式硬件·学习
那年窗外下的雪.1 小时前
Linux 学习笔记
linux·笔记·学习