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

相关推荐
LONGZETECH2 分钟前
无人机实训高成本痛点解法:虚拟仿真实现 70% 耗材损耗下降
大数据·算法·unity·架构·无人机
lifallen28 分钟前
Avernet:Agent 的组织网络
人工智能·学习·ai·开源软件·ai编程
农村小镇哥32 分钟前
怎么把HTM格式转化成WORD
ui·c#·word
农村小镇哥33 分钟前
C#读取CSV文件的方法
服务器·数据库·c#
知识分享小能手1 小时前
线性代数学习教程,从入门到精通,矩阵及其运算 — 完整知识点与详细推导(3)
学习·线性代数·矩阵
FuckPatience1 小时前
C# 报错参数无效
c#
小弥儿2 小时前
Firecrawl:把整个网页变成 AI 可查询的数据库
数据库·人工智能·学习
woshihuanglaoshi2 小时前
鸿蒙老旧项目升级改造高级:API废弃迁移/ArkTS语法升级/架构重构策略/增量迁移/风险管控方案
学习·华为·重构·架构·harmonyos·鸿蒙
zhangrelay2 小时前
ROS 2 Lyrical 第2章 ROS 2系统架构与核心概念
linux·笔记·学习·ubuntu·ros2
云空2 小时前
《软件专业完整学习路线图(本科4年+自学通用版,2026就业向)》
人工智能·科技·学习·计算机·编程·软件