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

相关推荐
y***54881 小时前
C++在游戏引擎中的开发
开发语言·c++·游戏引擎
charlie1145141911 小时前
使用 Poetry + VS Code 创建你的第一个 Flask 工程
开发语言·笔记·后端·python·学习·flask·教程
菥菥爱嘻嘻1 小时前
langchain学习-RAG+prompt+OutPutParse
学习·langchain·prompt
Jing_jing_X1 小时前
ChatGPT 四种模式:普通对话、推理思考、深度研究、学习模式有什么区别?
人工智能·学习·chatgpt
AA陈超1 小时前
Lyra项目中的输入系统
c++·笔记·学习·游戏·ue5·lyra
BuHuaX1 小时前
Unity_AssetBundle相关
unity·c#·游戏引擎·游戏策划
AA陈超1 小时前
ASC学习笔记0027:直接设置属性的基础值,而不会影响当前正在生效的任何修饰符(Modifiers)
c++·笔记·学习·ue5·虚幻引擎
doubao362 小时前
如何在海量文献中高效筛选有价值信息
人工智能·学习·自然语言处理·aigc·ai工具·ai检索
烤麻辣烫2 小时前
AI(新手)
人工智能·学习·机器学习·ai编程
青衫码上行2 小时前
【Java Web学习 | 第14篇】JavaScript(8) -正则表达式
java·前端·javascript·学习·正则表达式