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

相关推荐
石像鬼₧魂石8 小时前
Fail2Ban 一键部署 + 管理脚本(可直接执行)
linux·windows·学习·ubuntu
资料加载中8 小时前
【AIGC】SCAIL:通过对 3D 一致姿态表示进行上下文学习,实现工作室级角色动画
学习·aigc
石像鬼₧魂石8 小时前
Fail2Ban 实战终极速查表
linux·windows·学习·ubuntu
不定时总结的那啥8 小时前
Unity实现点击Console消息自动选中预制体的方法
unity·游戏引擎
缺点内向8 小时前
C# 中如何从 URL 下载 Word 文档:基于 Spire.Doc 的高效解决方案
开发语言·c#·word
代码游侠8 小时前
应用--Minishell实现
linux·运维·笔记·学习·算法
nnsix8 小时前
Unity OpenXR 关闭手柄的震动
unity·游戏引擎
重生之我在番茄自学网安拯救世界8 小时前
网络安全中级阶段学习笔记(七):Web 安全之文件上传漏洞笔记1(包含upload-labs-master靶场前三关实战)
笔记·学习·web安全·文件上传漏洞·网安基础
走在路上的菜鸟8 小时前
Android学Dart学习笔记第十五节 类
android·笔记·学习·flutter
xian_wwq8 小时前
【学习笔记】AI赋能安全运营中心典型场景
人工智能·笔记·学习