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

相关推荐
灰灰勇闯IT1 分钟前
TorchAir:PyTorch 跑在昇腾NPU上的桥梁
学习·aigc
Lucky_ldy2 分钟前
C语言学习:编译和链接
学习
chimchim667 分钟前
Azure ADF(Azure Data Factory 数据工厂)学习
学习·microsoft·azure
小新同学^O^8 分钟前
简单学习 --> Transformer架构
学习·架构·transformer
他们叫我阿冠11 分钟前
Docker的基础学习
学习·docker·容器
辰海Coding9 小时前
MiniSpring框架学习笔记-解决循环依赖的简化IoC容器
笔记·学习
加号39 小时前
【C#】 串口通信技术深度解析及实现
开发语言·c#
晓梦林9 小时前
cp520靶场学习笔记
android·笔记·学习
心中有国也有家10 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
Upsy-Daisy11 小时前
AI Agent 项目学习笔记(八):Tool Calling 工具调用机制总览
人工智能·笔记·学习