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

相关推荐
FPGA小迷弟6 分钟前
FPGA处理图像需要用到的主流接口详解
学习·fpga开发·verilog·fpga·modelsim
好奇龙猫7 分钟前
【日语学习-日语知识点小记-日本語体系構造-JLPT-N2前期阶段-第一阶段(10):単語文法】
学习
Hill_HUIL11 分钟前
学习日志19-不同VLAN间通信(3)-三层交换机
网络·学习
Hill_HUIL12 分钟前
学习日志21-路由概述
学习
Evonso23 分钟前
视频转码与切片(HLS)完整教程
c#
知识分享小能手23 分钟前
Oracle 19c入门学习教程,从入门到精通,VC++ + Oracle 实现汽配管理系统(21)
c++·学习·oracle
近津薪荼23 分钟前
优选算法——滑动窗口2(数组模拟哈希表)
c++·学习·算法
星火开发设计27 分钟前
using 关键字:命名空间的使用与注意事项
开发语言·c++·学习·算法·编程·知识
●VON31 分钟前
React Native for OpenHarmony:Pressable —— 构建下一代状态驱动交互的基石
学习·react native·react.js·性能优化·交互·openharmony
lfq76120433 分钟前
.NET Framework 下 C# MVC 项目敏感信息安全存储方法
安全·c#·mvc·.net