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

相关推荐
踏着七彩祥云的小丑1 小时前
AI学习——Gradio快速界面
人工智能·学习·ai
_oP_i1 小时前
105、word 出现 {TOCO“1-2“HZ}
开发语言·c#·word
「維他檸檬茶」1 小时前
大模型算法学习2026.6.1
学习·算法·大模型·nlp
江华森1 小时前
Git + Maven Java 项目部署实战全指南
运维·笔记·git·学习·maven
影寂ldy2 小时前
C#构造函数 + 析构函数
开发语言·c#
小陈phd2 小时前
多模态大模型学习笔记(四十三)—— 视觉定位(Visual Grounding):语言描述在图像中的精准锚定
笔记·学习·目标跟踪
Xpower 172 小时前
Codex 桌面端更新后 Chrome 插件和 Computer Use 不可用,怎么排查和修复
前端·人工智能·chrome·python·学习
aWty_4 小时前
实分析入门(11)--Cantor三分集
学习·数学·算法·实变函数
for_ever_love__11 小时前
UI学习:UISearchController基础了解和应用
学习·ui·ios·objective-c
心中有国也有家11 小时前
GE图引擎深度解析——CANN的计算图优化与执行引擎
人工智能·pytorch·python·学习·numpy