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 小时前
Java网络编程学习笔记,从网络编程三要素到TCP/UDP协议
java·网络·学习
Traced back8 小时前
# C# + SQL Server 实现自动清理功能的完整方案:按数量与按日期双模式
开发语言·c#
Quintus五等升8 小时前
深度学习④|分类任务—VGG13
人工智能·经验分享·深度学习·神经网络·学习·机器学习·分类
二哈喇子!9 小时前
Java框架精品项目【用于个人学习】
java·spring boot·学习
JIes__9 小时前
Unity(二)——MonoBehavior中的重要内容
unity·游戏引擎
yj爆裂鼓手9 小时前
c#万能变量
开发语言·c#
Mixtral9 小时前
2026年4款学习转写工具测评:告别逐字整理,自动生成复习资料
笔记·学习·ai·语音转文字
鄭郑10 小时前
【playwright 学习笔记】原理讲解与基础操作 --- day01
笔记·学习
不绝19110 小时前
C#进阶:委托
开发语言·c#
喜欢喝果茶.10 小时前
跨.cs 文件传值(C#)
开发语言·c#