核心功能实现 区域检测
使用 PartZone 模块创建检测区域(名为 “water”)(可以使用ZonePlus来实现) 当玩家进入指定 Part(Sea)时激活游泳状态 玩家离开区域时恢复正常状态 游泳状态控制
1 2 3 4 5 6 -- 进入水体时 humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) -- 禁用跳跃 humanoid:ChangeState(Enum.HumanoidStateType.Swimming) -- 强制游泳状态 -- 离开水体时 humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true) -- 恢复跳跃能力 使用LinearVelocity推动人物移动
创建 LinearVelocity 对象施加推进力 1 2 3 Swim = Instance.new("LinearVelocity") Swim.MaxAxesForce = Vector3.new(3000,3000,3000) Swim.Attachment0 = Root.RootAttachment -- 绑定到角色根部件 游泳运动机制
1 2 3 4 function Swimming() -- 按移动方向和步行速度推进 Swim.VectorVelocity = Humanoid.MoveDirection * Humanoid.WalkSpeed end 关键代码设计 状态管理优化
动态创建/销毁 LinearVelocity 对象(Swim) 离开水域时通过 Swim:Destroy() 释放资源 每帧运行
1 2 3 RS.PreRender:Connect(function() if Swim then Swimming() end -- 每帧更新游泳动力 end) 利用 PreRender 事件保证流畅的水中移动 帧率无关的物理更新 玩家的状态设置
...