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

相关推荐
王夏奇3 分钟前
python-pytest学习
python·学习·pytest
祁鱼鱼鱼鱼鱼5 分钟前
Nginx源码编译及平滑升级及回滚
学习
AnalogElectronic7 分钟前
云原生学习day1ubuntu安装docker,基础镜像打包
学习·docker·云原生
weixin_4588726119 分钟前
东华复试OJ二刷复盘6
学习
ellis197024 分钟前
Unity资源管理框架Addressables[五] 构建
unity
微露清风29 分钟前
系统性学习Linux-第四讲-进程控制
linux·服务器·学习
01二进制代码漫游日记30 分钟前
C/C++中的内存区域划分
c语言·jvm·数据结构·学习
HAREWORK_FFF32 分钟前
用CAIE认证为简历加分:AI学习者的标准学习周期与规划
人工智能·学习·百度
jjjxxxhhh12339 分钟前
[项目]-搭建一个git服务器呢,完整详细的落地方案
学习
小曹要微笑42 分钟前
C#的运算符重载
开发语言·c#·运算符重载·c#运算符重载