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

相关推荐
北顾笙9801 小时前
LLM学习-day04
学习
郝学胜-神的一滴2 小时前
罗德里格斯旋转公式(Rodrigues‘ Rotation Formula)完整推导
c++·unity·godot·图形渲染·three.js·unreal
lzj_pxxw3 小时前
W25Q64存储芯片 软件设计刚需常识
stm32·单片机·嵌入式硬件·mcu·学习
Slow菜鸟3 小时前
AI学习篇(四) | AI设计类Skills推荐清单(2026年)
人工智能·学习
念恒123063 小时前
Python(列表进阶)
python·学习
QYQ_11274 小时前
嵌入式学习——杂项设备、Platform总线和设备树源文件
学习
宝桥南山5 小时前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore
wuxinyan1236 小时前
大模型学习之路03:提示工程从入门到精通(第三篇)
人工智能·python·学习
十安_数学好题速析6 小时前
【多选】曲线方程:四步避坑判断曲线类型
笔记·学习·高考
千寻girling7 小时前
五一劳动节快乐 [特殊字符][特殊字符][特殊字符]
java·c++·git·python·学习·github·php