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

相关推荐
風清掦2 分钟前
【江科大STM32学习笔记-11】SPI通信协议 - 11.2 软件SPI读写W25Q64
笔记·stm32·单片机·嵌入式硬件·学习
披着羊皮不是狼7 分钟前
Git完整学习总结
git·学习·elasticsearch
我是发哥哈11 分钟前
主流AI培训机构能力横向评测:核心维度与选型要点解析
大数据·人工智能·学习·机器学习·ai·chatgpt·aigc
molong93118 分钟前
SIM 卡监听(电话监听)
android·学习·kotlin
小宋加油啊19 分钟前
claude学习
学习
DeepModel31 分钟前
机器学习数据预处理:特征构造
人工智能·学习·算法·机器学习
qq_3482318542 分钟前
企业级避坑指南
人工智能·学习
我的征途是星辰大海。1 小时前
设计模式(学习笔记)(第一章)
笔记·学习·设计模式
bitt TRES1 小时前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互
qeen871 小时前
【算法笔记】差分与经典例题解析
c语言·c++·笔记·学习·算法·差分