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

相关推荐
Arctic.acc4 分钟前
Datawhale:HelloAgent,学习打卡6
学习
LO嘉嘉VE10 分钟前
学习笔记十七:神经网络基础概念
笔记·神经网络·学习·机器学习
不羁的木木41 分钟前
【开源鸿蒙跨平台开发学习笔记】Day02:React Native 开发 HarmonyOS-环境搭建篇(填坑记录)
笔记·学习·react native·harmonyos·har
Tan_Ying_Y1 小时前
synchronized和ReentrantLock的区别是什么?他们的底层原理是什么?
开发语言·c#
gc_22991 小时前
学习C#调用Microsoft.Office.Interop.Word将Word转换为html
c#·html·word·interop.word
唐青枫2 小时前
C#.NET 集合表达式详解:新时代的集合初始化方式
c#·.net
云栈开源日记3 小时前
Python 开发技术栈梳理:从数据库、爬虫到 Django 与机器学习
数据库·爬虫·python·学习·机器学习·django
青衫码上行3 小时前
【Java Web学习 | 第15篇】jQuery(万字长文警告)
java·开发语言·前端·学习·jquery
必胜的思想钢印8 小时前
修改主频&睡眠模式&停机模式&待机模式
笔记·stm32·单片机·嵌入式硬件·学习
jtymyxmz8 小时前
《Unity Shader》8.4 透明度混合
unity·游戏引擎