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

相关推荐
唐宋元明清21881 小时前
.NET 磁盘管理-技术方案选型
windows·c#·存储
故事不长丨2 小时前
C#正则表达式完全攻略:从基础到实战的全场景应用指南
开发语言·正则表达式·c#·regex
气概3 小时前
法奥机器人学习使用
学习·junit·机器人
Qhumaing4 小时前
C++学习:【PTA】数据结构 7-1 实验7-1(最小生成树-Prim算法)
c++·学习·算法
好大哥呀4 小时前
Java Web的学习路径
java·前端·学习
梦雨羊6 小时前
Base-NLP学习
人工智能·学习·自然语言处理
丝斯20116 小时前
AI学习笔记整理(42)——NLP之大规模预训练模型Transformer
人工智能·笔记·学习
小猪佩奇TONY6 小时前
Linux 内核学习(14) --- linux x86-32 虚拟地址空间
linux·学习
副露のmagic7 小时前
更弱智的算法学习 day28
学习
ha20428941947 小时前
Linux操作系统学习记录之---TcpSocket
linux·网络·c++·学习