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

相关推荐
尊治2 小时前
变频器学习入门教程
学习·esim电工仿真·电工仿真软件·电工仿真·电工接线学习·esim电工制图
tyqtyq222 小时前
旅行打包清单 App — HarmonyOS AI 应用开发技术博客
人工智能·学习·华为·生活·harmonyos
辞旧 lekkk3 小时前
【Redis初阶】常见数据类型
开发语言·数据库·c++·redis·学习·缓存·bootstrap
HH‘HH3 小时前
Unity 项目创建标准指南:分辨率、尺寸、文件路径与命名规范
unity·游戏引擎
小弥儿4 小时前
GitHub今日热榜 | 2026-07-17:教育Agent与极低量化分庭抗礼
学习·开源·github
dtq04245 小时前
C语言-结构体详解
c语言·开发语言·学习
元气少女小圆丶6 小时前
unity发布web嵌入到前端页面的接受参数
前端·unity·webgl
天选之子1236 小时前
DuckDB学习
jvm·学习
HH‘HH6 小时前
Unity 打包程序设置及注意事项全攻略
unity·游戏引擎
一只小菜鸡..7 小时前
Stanford CS144 学习笔记 (四):网络拥塞控制与 AIMD 算法
网络·笔记·学习