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

相关推荐
专业开发者3 分钟前
Wi-Fi 技术学习:Wi-Fi 射频设计核心原则 —— 只覆盖你想要的区域
学习
qq_150841991 小时前
3天基于VS2026的C#编程入门及动态调用CH341DLLA64读写I2C从机
开发语言·c#
码农葫芦侠3 小时前
Rust学习教程2:基本语法
开发语言·学习·rust
LYS_06183 小时前
c++学习(1)(编译过程)
c++·学习
happymaker06264 小时前
Java学习日记——DAY25(JavaSE完结)
java·开发语言·学习
专业开发者4 小时前
Wi-Fi 技术学习:面向 Wi-Fi 网络的抗干扰技术
网络·学习
努力学习的小廉4 小时前
redis学习笔记(五)—— set 数据类型
redis·笔记·学习
溪水西流5 小时前
NodifyEditor Zoom 机制分析
开发语言·c#·avalonia
好奇龙猫5 小时前
【日语学习-日语知识点小记-日本語体系構造-JLPT-N2前期阶段-第一阶段(13):単語文法】
学习
淡海水5 小时前
【节点】[ScleraLimbalRing节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·ring·limbal·sclera