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

相关推荐
AvocadoZeroOneOI1 小时前
Kadane 算法(最大子段和)
c++·学习·算法·动态规划
楚山之石8 小时前
扩散时间 Δ 与脉宽 δ——TE 缩短如何提高 b 值 SNR
图像处理·学习·mri·医学影像
Hhy_11078 小时前
【Linux 进击之路 · 01】起航篇:从认识操作系统到XShell 远程连接、多叉树目录体系到高频基础指令全解析
linux·服务器·后端·学习
小贺儿开发10 小时前
Unity 轻量级外部素材加载器(热更新)
科技·学习·unity·程序员·工具·热更新·ugui
kyrie_sakura10 小时前
大模型学习28 -- 模型原理2 -- LLM架构
学习·架构
千寻girling13 小时前
1.深度学习 | 尚硅谷学习
人工智能·学习
Century_Dragon19 小时前
把规范练在实操之前——智能网联汽车传感器测试装调教学软件
学习
kyrie_sakura1 天前
大模型学习27 -- 模型原理1 -- 发展历程
学习
mex_wayne1 天前
LeRobot SO-ARM101 学习笔记(6) pi0.5--论文阅读
论文阅读·笔记·学习·pi0.5
kaixin_啊啊1 天前
【DNA数据学习】从DNA序列到模型预测
学习·生物信息