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

相关推荐
张人玉1 分钟前
西门子 S7 PLC 通信 WPF 应用分析笔记
笔记·c#·wpf·plc
山土成旧客6 分钟前
【Python学习打卡-Day35】从黑盒到“玻璃盒”:掌握PyTorch模型可视化、进度条与推理
pytorch·python·学习
@zulnger6 分钟前
python 学习笔记(循环)
笔记·python·学习
Shannon Law11 分钟前
【免费下载】关于机器学习和深度学习的书籍
学习
Master_oid11 分钟前
机器学习28:增强式学习(Deep Reinforcement Learn)③
人工智能·学习·机器学习
刘975316 分钟前
【第22天】22c#今日小结
开发语言·c#
我命由我1234518 分钟前
开发中的英语积累 P25:Axis、Stroke、Corner、Interceptor、Declared、Internal
经验分享·笔记·学习·职场和发展·求职招聘·职场发展·学习方法
张人玉19 分钟前
整合 Sugar ORM 连接 SQLite 数据库到 WPF 折线图项目
数据库·sqlite·c#·wpf
扑火的小飞蛾29 分钟前
【Ansible学习笔记01】 批量执行 shell 命令
笔记·学习·ansible
呆呆敲代码的小Y33 分钟前
【Unity 实用工具篇】| UX Tool 工具 快速上手使用,提高日常开发效率
游戏·unity·游戏引擎·游戏程序·ux