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

相关推荐
斯维赤14 小时前
Python学习超简单第十一弹:邮件发送
开发语言·python·学习
2401_8274999915 小时前
数据分析学习06(黑马)-Matplotlib
学习·数据分析·matplotlib
Magic@15 小时前
Redis学习[1] ——基本概念和数据类型
linux·开发语言·数据库·c++·redis·学习
AIKZX15 小时前
西门子博途 TIA Portal v18 中文版图文安装教程(超级详细)附下载链接
开发语言·c#·编辑器·idea
BJHHZX199115 小时前
北京华恒智信助力传统制造与高新技术行业完成存量人才激活与组织效能重塑
学习
阿标的博客16 小时前
Python学习(三):Python程序的运行方式
开发语言·python·学习
斯维赤16 小时前
每天学习一个小算法:堆排序
学习·算法·排序算法
Edward1111111116 小时前
4月25泛型
学习
南境十里·墨染春水16 小时前
linux学习进展 线程同步——条件变量
java·开发语言·学习
zhangyueping838516 小时前
大模型学习笔记-AI通识
人工智能·笔记·学习