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

相关推荐
请你喝好果汁64125 分钟前
玉米细胞类型特异性顺式调控的遗传结构
学习
Mr YiRan29 分钟前
C++高级之SLT中的容器学习与函数谓词
开发语言·c++·学习
小贺儿开发35 分钟前
Unity3D 自动化物流分拣模拟
运维·科技·unity·自动化·人机交互·传送带·物流分拣
xhyu6136 分钟前
【学习笔记】推荐系统 (5.排序:多目标模型、MMoE、融合预估分数、视频播放建模)
笔记·学习·音视频
bepeater123437 分钟前
Laravel9.X核心特性全面解析
c语言·c++·c#·php
EQ-雪梨蛋花汤39 分钟前
【3D可视化】基于 Unity 的智慧体育馆三维信息可视化大屏实践
3d·unity·信息可视化
悠哉悠哉愿意1 小时前
【物联网学习笔记】中断
笔记·单片机·嵌入式硬件·物联网·学习
snowfoootball1 小时前
优先队列/堆 题目讲解
学习·算法
weixin_424294671 小时前
Unity 使用Steamworks.NET
unity·游戏引擎
shangjian0071 小时前
AI-大语言模型LLM-LangChainV1.0学习笔记-OpenAI类和ChatOpenAI类
人工智能·学习·语言模型