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

相关推荐
周杰伦fans4 分钟前
C#中OrderByDescending 是LINQ扩展方法之一
开发语言·c#·linq
jtymyxmz10 分钟前
《Unity Shader》10.2.1 镜子效果
unity·游戏引擎
ellis197012 分钟前
Unity打开新项目Package相关报错处理记录
unity
周杰伦fans27 分钟前
C# 中 SQL Server 数据库调优指南(小白友好版)
开发语言·数据库·c#
李小星同志31 分钟前
HTTM: Head-wise Temporal Token Merging for Faster VGGT论文学习
学习
喜欢吃燃面1 小时前
算法竞赛中的堆
c++·学习·算法
微:xsooop2 小时前
iOS 上架4.3a 审核4.3a 被拒4.3a 【灾难来袭】
flutter·unity·ios·uniapp
自然语2 小时前
人工智能之数字生命-学习的过程
数据结构·人工智能·深度学习·学习·算法
gc_22992 小时前
学习C#调用AspNetCoreRateLimit包限制客户端访问次数(4:源码分析)
c#·coreratelimit
你好~每一天2 小时前
从传统行业到AI入门:我的CAIE Level I学习体验与思考
大数据·数据结构·人工智能·学习·jupyter·idea