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

相关推荐
秦哈哈23 分钟前
【Hello Agents】学习笔记(二)
笔记·学习·microsoft
sunshine22 girl1 小时前
Java学习三 高级语法3, 方法
java·学习
传奇开心果编程1 小时前
【dioxus0.7基础语法学与练】第8课:RSX 宏 — Dioxus 声明式 UI 语法
开发语言·学习·rust
一条破秋裤2 小时前
24_MPU6050结构参数与寄存器基础
stm32·嵌入式硬件·学习
诗词在线3 小时前
江上渔者|原文+注释+译文+赏析+考点
学习
逆境不可逃3 小时前
Pi Agent 学习笔记:不同模型如何接入同一套工具循环
笔记·学习
词却3 小时前
OpenCV学习:dlib 人脸检测
人工智能·opencv·学习
爱吃苹果的日记本3 小时前
数据结构第三课补充(空间复杂度)
数据结构·学习
PC2005-cloud4 小时前
Rust学习笔记:所有权、借用与生命周期——Rust的核心机制
笔记·学习·rust
yswenli4 小时前
基于C#开发网站视频嗅探下载工具——BrowserVideoGrabber
c#·videodownload·webvideodownload·videograbber