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

相关推荐
AI成长日志8 小时前
【datawhale】hello agents开源课程学习记录第5章 智能体应用实践:低代码平台构建指南
学习·低代码·开源
唐青枫9 小时前
C#.NET ReaderWriterLockSlim 深入解析:读写锁原理、升级锁与使用边界
c#·.net
GHL2842710909 小时前
RAG相关问题整理学习
学习·ai
张老师带你学11 小时前
UnityVR弯曲UI
科技·游戏·unity·游戏引擎·模型
张老师带你学12 小时前
unity作业,街角小场景
科技·游戏·unity·游戏引擎·模型
知识分享小能手13 小时前
MongoDB入门学习教程,从入门到精通,MongoDB创建、更新和删除文档(3)
数据库·学习·mongodb
诗句藏于尽头13 小时前
基于GPT2的底模微调实现微信聊天风格模仿输出
学习·微信
%小农14 小时前
学习cursor
学习
还是做不到嘛\.14 小时前
DVWA靶场-Brute Force
运维·服务器·数据库·学习
chudonghao15 小时前
[UE学习笔记][基于源码] 理解 Gameplay
c++·笔记·学习·ue5