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

相关推荐
冯诺依曼的锦鲤18 小时前
算法练习:差分
c++·学习·算法
WarPigs19 小时前
Unity编辑器开发笔记
unity·编辑器·excel
im_AMBER19 小时前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
赵文宇(温玉)19 小时前
不翻墙,基于Rancher极速启动Kubernetes,配置SSO登录,在线环境开放学习体验
学习·kubernetes·rancher
Q***f63520 小时前
C++在游戏引擎开发中的实践
游戏引擎
光头闪亮亮20 小时前
电子发票解析工具-c#桌面应用开发-DataGridView表格控件使用详解
c#
6***x54520 小时前
C++在计算机视觉中的图像处理
c++·图像处理·计算机视觉·游戏引擎·logback·milvus
周杰伦fans21 小时前
C# 中的 `Hashtable`
开发语言·c#
lingggggaaaa21 小时前
免杀对抗——C2远控篇&PowerShell&有无文件落地&C#参数调用&绕AMSI&ETW&去混淆特征
c语言·开发语言·笔记·学习·安全·microsoft·c#
咩图21 小时前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism