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

相关推荐
bugcome_com12 小时前
bugcome —— 个人独立开发 6 年商业计划书
c#
知识分享小能手12 小时前
SQL Server 2019入门学习教程,从入门到精通,SQL Server 2019 视图操作 — 语法知识点及使用方法详解(16)
sql·学习·sqlserver
前路不黑暗@13 小时前
Java项目:Java脚手架项目的 B 端用户服务(十四)
android·java·开发语言·spring boot·笔记·学习·spring cloud
锅包一切13 小时前
PART17 一维动态规划
c++·学习·算法·leetcode·动态规划·力扣·刷题
科技林总14 小时前
【系统分析师】9.3 通信与网络安全技术
学习
CappuccinoRose16 小时前
CSS 语法学习文档(十五)
前端·学习·重构·渲染·浏览器
lambda16 小时前
游戏开发者的CMU动作数据使用指南:如何将AMC文件转Unity动画
unity·游戏开发·动作捕捉·cmu
bugcome_com17 小时前
C# 中的封装与访问修饰符
c#
香芋Yu17 小时前
【大模型面试突击】04_Embedding与表示学习
学习·面试·embedding
淡海水18 小时前
【节点】[ComputeVertexPosition节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·position·compute·vertex