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

相关推荐
q***d17326 分钟前
后端缓存技术学习资源,Redis+Memcached
redis·学习·缓存
因为奋斗超太帅啦2 小时前
Git分布式版本控制工具学习笔记(一)——git本地仓库的基本使用
笔记·git·学习
Jeled2 小时前
RecyclerView ViewHolder 复用机制详解(含常见错乱问题与优化方案)
android·学习·面试·kotlin
可可苏饼干2 小时前
LVS服务器
linux·运维·笔记·学习·lvs
四谎真好看2 小时前
Java 黑马程序员学习笔记(进阶篇27)
java·开发语言·笔记·学习·学习笔记
唐青枫3 小时前
.NET Web 应用 Linux 部署全指南:从环境搭建到生产上线
c#·.net
神秘的土鸡3 小时前
【CS创世SD NAND征文】为无人机打造可靠数据仓:工业级存储芯片CSNP32GCR01-AOW在飞控系统中的应用实践
嵌入式硬件·游戏引擎·无人机·cocos2d·雷龙
gc_22999 小时前
学习C#调用OpenXml操作word文档的基本用法(7:Style类分析-5)
学习·word·openxml
AA陈超9 小时前
ASC学习笔记0014:手动添加一个新的属性集
c++·笔记·学习·ue5