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

相关推荐
编程版小新5 分钟前
Day1:体验产品,以画图方式梳理用户操作和管理员操作
学习
ellis19701 小时前
Lua的module和require
unity·lua
三品吉他手会点灯1 小时前
STM32F103 学习笔记-24-I2C-读写EEPROM(第2节)-I2C协议层介绍
笔记·stm32·学习
z200509301 小时前
【C++学习】C++ 类型转换深度解析:从 C 风格缺陷到 C++ 四种安全转换的思想内核
c语言·c++·学习
三品吉他手会点灯1 小时前
STM32F103 学习笔记-24-I2C-读写EEPROM(第3节)-STM32的I2C框图详解
笔记·stm32·学习
踏着七彩祥云的小丑1 小时前
嵌入式测试学习第 36 天:串口日志分析、通过日志定位简单问题
单片机·嵌入式硬件·学习
MartinYeung52 小时前
[论文学习]LLM 情境学习资料的快速精确遗忘技术:基于 In-Context Learning 与量化 K-Means 的 ERASE 方法
学习·算法·kmeans
淡海水2 小时前
38-Hybrid生态-LeanCLR总览
unity·架构·c#·热更新·clr·hybrid·leanclr
踏着七彩祥云的小丑2 小时前
Go学习第8天:接口 + 泛型 + 错误处理
开发语言·学习·golang·go
fanged2 小时前
高通学习12--调试工具(TODO)
学习