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

相关推荐
A9better36 分钟前
C++——指针与内存
c语言·开发语言·c++·学习
ICscholar42 分钟前
具身智能‘Affordance‘理解
人工智能·学习·算法
淡海水1 小时前
【节点】[EyeSurfaceTypeDebug节点]原理解析与实际应用
unity·游戏引擎·debug·shadergraph·图形·surface·eye
listhi5202 小时前
基于C#实现动态人脸检测
开发语言·c#
今儿敲了吗2 小时前
18| 差分数组
c++·笔记·学习·算法
浅念-2 小时前
C++ 模板初阶:从泛型编程到函数模板与类模板
c语言·开发语言·数据结构·c++·笔记·学习
知识分享小能手2 小时前
SQL Server 2019入门学习教程,从入门到精通,SQL Server 2019 创建和使用索引 — 语法知识点及使用方法详解(12)
数据库·学习·sqlserver
前路不黑暗@3 小时前
Java项目:Java脚手架项目的模板服务和网关服务的实现(三)
java·开发语言·spring boot·git·学习·spring cloud·maven
寒秋花开曾相惜4 小时前
(学习笔记)2.1 信息存储(2.1.1 十六进制表示法)
笔记·学习
-To be number.wan5 小时前
算法学习日记 |贪心算法
c++·学习·算法·贪心算法