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

相关推荐
Yuk丶18 分钟前
Procedural Dialogue Engine - UE4程序化对话系统的技术实现
c++·游戏引擎·ue4·游戏程序·虚幻
自信1504130575923 分钟前
重生之从0开始学习c++之string(上)
开发语言·c++·学习
南境十里·墨染春水39 分钟前
linux学习进展 线程
java·linux·学习
久菜盒子工作室1 小时前
TCL是哪个板块的,去年大涨的原因是什么
人工智能·学习
笨鸟先飞的橘猫1 小时前
Mysql——MVCC学习
数据库·学习·mysql
jiayong231 小时前
第 33 课:任务看板视图(按状态分列)与本地持久化
开发语言·前端·javascript·学习
Accerlator2 小时前
MCP vs Function Calling
学习
夜瞬2 小时前
从后端到 RAG 再到 Agent:一份可执行的大模型应用开发学习路线
学习·语言模型
RReality2 小时前
【Unity Shader URP】屏幕空间扭曲后处理(Screen Space Distortion)实战教程
ui·unity·游戏引擎·图形渲染·材质
qeen872 小时前
【算法笔记】二分查找与二分答案
c语言·c++·笔记·学习·算法·二分