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

相关推荐
MartinYeung51 分钟前
[论文学习]Latent Fusion Jailbreak: 融合有害与无害表征以诱导大语言模型产生不安全输出
学习·安全·语言模型
星恒随风2 分钟前
C++ 哈希详解(二):开放定址、哈希桶与 C++ 哈希表底层实现
c++·笔记·学习·哈希算法·散列表
迪丽热爱5 分钟前
每日英语-17
学习
ellis19701 小时前
u3d插件xLua[十] lua侧判空问题
unity
打工陈2 小时前
电路理论知识学习--(4)RC、LC、LR低通滤波对比
学习·智能硬件
Hhy_11072 小时前
《C++深度解构02》类和对象(上)——定义规范、内存对齐与 this 指针全解
c语言·开发语言·c++·学习·visual studio
我爱cope2 小时前
【计算机网络 | 数据链路层3:MAC 地址是什么?它和 IP 地址有什么区别?】
学习·tcp/ip·计算机网络·智能路由器·数据链路层
电子云与长程纠缠2 小时前
UE5 Lyra PocketWorld进行3D内容UI预览 - 下
开发语言·学习·游戏·ui·ue5
poiu12346572 小时前
视频离线观看用什么软件方便?2026多场景工具实测对比
学习
爱编程的Zion2 小时前
Jenkins 从 0 到 1 学习笔记
笔记·学习·jenkins