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

相关推荐
YangYang9YangYan1 分钟前
数据科学与大数据技术专业学习数据分析的价值
大数据·学习·数据分析
70asunflower8 分钟前
【Day1-2】部署&运行 Gemma4 大模型 - Datawhale AI学习
人工智能·学习·datawhale·amdev
caimouse20 分钟前
Godot Engine 最新版官方文档(简体中文完整翻译 & 精简梳理)
游戏引擎·godot
库奇噜啦呼36 分钟前
【iOS】源码学习-锁的原理
学习·ios·cocoa
_李小白40 分钟前
【android opencv学习笔记】Day 33: 直线检测之图像轮廓检测
android·opencv·学习
AI_零食42 分钟前
HarmonyOS 表单验证机制深度解析与实践
学习·华为·harmonyos·鸿蒙·鸿蒙系统
云草桑1 小时前
.NET10+AI 架构师全套实战学习文档(含源码、案例、面试题、项目源码)
人工智能·学习·ai·.net
小满Autumn1 小时前
固高GTS运动控制卡 — C#开发完全指南
c#·.net·上位机·运动控制卡
暗夜猎手-大魔王1 小时前
hermes源码学习5-Provider 运行时解析
大数据·人工智能·学习
-To be number.wan1 小时前
计算机组成原理 | 指令寻址
学习·计算机组成原理