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

相关推荐
浩瀚地学8 小时前
【Java】JDK8的一些新特性
java·开发语言·经验分享·笔记·学习
JeffDingAI8 小时前
【Datawhale学习笔记】深入大模型架构
笔记·学习
暖阳之下9 小时前
学习周报三十一
学习
我是苏苏10 小时前
C#高级:使用ConcurrentQueue做一个简易进程内通信的消息队列
java·windows·c#
Master_oid10 小时前
机器学习29:增强式学习(Deep Reinforcement Learning)④
人工智能·学习·机器学习
楼田莉子11 小时前
Linux学习之磁盘与Ext系列文件
linux·运维·服务器·c语言·学习
一条闲鱼_mytube11 小时前
智能体设计模式(三)多智能体协作-记忆管理-学习与适应
人工智能·学习·设计模式
Never_Satisfied11 小时前
C#获取汉字拼音字母方法总结
开发语言·c#
丝斯201112 小时前
AI学习笔记整理(50)——大模型中的Graph RAG
人工智能·笔记·学习
一允12 小时前
Git学习记录
git·学习