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

相关推荐
一定要AK3 小时前
刷题时的学习笔记
c++·笔记·学习
xxxibolva4 小时前
SQL 学习
数据库·sql·学习
星辰即远方7 小时前
OC学习Foudation框架
学习·ios·objective-c
weixin_408099677 小时前
图片去水印 API 接口实战:网站如何实现自动去水印(Python / PHP / C#)
图像处理·人工智能·python·c#·php·api·图片去水印
yyk的萌7 小时前
AI 应用开发工程师基础学习计划
开发语言·python·学习·ai·lua
龘龍龙9 小时前
大模型学习(三)-RAG、LangChain
学习·langchain
The森9 小时前
cocos2d-x棋牌项目-模块2:GameView、Node 与 zOrder
游戏引擎·cocos2d
计算机安禾10 小时前
【数据结构与算法】第22篇:线索二叉树(Threaded Binary Tree)
c语言·开发语言·数据结构·学习·算法·链表·visual studio code
:mnong10 小时前
Superpowers 项目设计分析
java·c语言·c++·python·c#·php·skills
我是唐青枫10 小时前
C#.NET 分布式事务 深入解析:TCC、Saga、Outbox 与落地取舍
分布式·c#·.net