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

相关推荐
Bechamz8 小时前
大数据开发学习Day25
java·大数据·学习
鸽子一号8 小时前
c#Modbus通信
开发语言·c#
zhangrelay9 小时前
云课实践速通系列-基础篇汇总-必修-通识基础和专业基础-2026--工科--自动化、电气、机器人、测控等
linux·笔记·单片机·学习·ubuntu·机器人·自动化
计算机安禾9 小时前
【计算机网络】第5篇:网桥学习与生成树算法——环路拓扑中的路径收敛问题
学习·计算机网络·算法
知识分享小能手9 小时前
R语言入门学习教程,从入门到精通,R语言局部整体型数据可视化(10)
学习·信息可视化·r语言
承渊政道10 小时前
【动态规划算法】(完全背包问题从状态定义到空间优化)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
玩转单片机与嵌入式10 小时前
学习嵌入式AI(TInyML),只需掌握这点python基础即可!
人工智能·python·学习
ErizJ10 小时前
Linux|学习笔记
linux·笔记·学习
大数据三康10 小时前
Java字符统计:从输入到输出的完整解析
java·学习·循环结构
9523611 小时前
SpringAOP
java·后端·学习·spring