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

相关推荐
江沉晚呤时24 分钟前
深入解析 ASP.NET Core 中的 ResourceFilter
开发语言·c#·.net·lucene
XiaoyuEr_66881 小时前
如何创建一个C#项目(基于VS2022版)
开发语言·c#
LVerrrr1 小时前
Missashe考研日记-day27
学习·考研
虾球xz2 小时前
游戏引擎学习第240天:将渲染器移至第三层
c++·学习·游戏引擎
BruceNeter3 小时前
C# 使用StackExchange.Redis实现分布式锁的两种方式
redis·c#
小王努力学编程4 小时前
【Linux网络编程】应用层协议HTTP(实现一个简单的http服务)
linux·服务器·网络·c++·网络协议·学习·http
viperrrrrrrrrr74 小时前
大数据学习(112)-HIVE中的窗口函数
hive·sql·学习
*TQK*4 小时前
CSS学习笔记8——表格
css·笔记·学习·html
林枫依依5 小时前
Unity Webgl在编辑器中报错:Cannot connect to destination host
unity·编辑器·webgl
KhalilRuan5 小时前
Unity-Shader详解-其一
unity·游戏引擎