第八天 AI开发:NavMesh导航系统 对话系统:使用ScriptableObject存储对话数据 存档系统:JSON序列化保存数据

一、智能导航系统:NavMesh实战指南

1.1 导航网格基础配置

  1. 在Unity编辑器中:

    • 选择场景中的静态物体
    • 勾选Navigation Static属性
    • 打开Window > AI > Navigation窗口
  2. 烘焙参数设置:

    NavMeshBuildSettings settings = NavMesh.GetSettingsByID(0);
    settings.agentRadius = 0.5f;    // 代理半径
    settings.agentHeight = 2.0f;    // 代理高度
    settings.agentSlope = 45;       // 最大坡度
    NavMeshBuilder.BuildNavMeshAsync(settings);
    

1.2 NavMeshAgent组件控制

public class NPCMovement : MonoBehaviour
{
   
    public Transform target;
    private NavMeshAgent agent;
    
    void Start()
    {
   
        agent = GetComponent<NavMeshAgent>();
        agent.speed = 3.5f;       // 移动速度
        agent.angularSpeed = 360;  // 旋转速度
        agent.stoppingDistance = 1f; // 停止距离
    }

    void Update()
    {
   
        if(target != null)
        {
   
            agent.SetDestination(target.position);
            
            // 到达目标后执行动作
            if(!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
            {
   
                OnReachDestination();
            }
        }
    }

    void OnReachDestination()
    {
   
        // 触发对话或执行其他逻辑
    }
}

1.3 动态障碍物处理

// 动态添加障碍物
NavMeshObstacle obstacle = gameObject.AddComponent<NavMeshObstacle>();
obstacle.shape = NavMeshObstacleShape.Capsule;
obstacle.carving = true;  // 实时更新导航网格
obstacle.size = new Vector3(1f, 2f, 1f);

二、对话系统:ScriptableObject高级应用

2.1 对话数据结构设计

[CreateAssetMenu(fileName = "New Dialogue", menuName = "Dialogue System/Dialogue")]
public class DialogueData : ScriptableObject
{
   
    [System.Serializable]
    public class DialogueNode
    {
   
        public string speakerName;
        [TextArea(3,5)] public string content;
        public DialogueOption[] options;
    }

    [System.Serializable]
    public class DialogueOption
    {
   
        public string optionText;
        public DialogueData nextDialogue;
    }

    public DialogueNode[] dialogueNodes;
}

2.2 对话管理器实现

public class DialogueManager : MonoBehaviour
{
   
    public static DialogueManager Instance;
    public
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值