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

相关推荐
MY_TEUCK2 小时前
【2026最新Python+AI学习基础】Python 入门笔记篇
笔记·python·学习
qq_571099353 小时前
学习周报四十五
学习
鱼很腾apoc4 小时前
【学习篇】第20期 超详解 C++ 多态:从语法规则到底层原理
java·c语言·开发语言·c++·学习·算法·青少年编程
地狱为王6 小时前
Unity实现猫脸关键点检测
unity·游戏引擎·猫脸关键点检测
子繁~~7 小时前
AI工具学习
学习
叫我少年8 小时前
C#基础数据类型
c#·数据类型
Terrence Shen8 小时前
Claude Code Harness 源码学习讲义
linux·学习·ubuntu
南境十里·墨染春水9 小时前
守护进程编程流程
linux·学习
唐青枫9 小时前
C#.NET YARP 跨域配置详解:网关统一处理 CORS
c#·.net
GEO从入门到精通10 小时前
学习GEO资料要多久能看到效果?
人工智能·学习