Advertisement
Guest User

Untitled

a guest
May 26th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. -- example stuff just trying it out.
  2.  
  3. --[[ Services ]]--
  4. local players = game:GetService("Players")
  5. local run_service = game:GetService("RunService")
  6. local input_service = game:GetService("UserInputService")
  7. local action_service = game:GetService("ContextActionService")
  8.  
  9. --[[ Variables ]]--
  10. local base_height = -50 -- change this in future
  11. local top_height = 40-- change this in future
  12. local is_swimming = false
  13. local platform_stand_enabled = false
  14. local collision_counter = 0
  15.  
  16. local player = players.LocalPlayer
  17. local camera = workspace.CurrentCamera
  18. local character = player.Character or player.CharacterAdded:Wait()
  19. local root_part = character:WaitForChild("HumanoidRootPart")
  20. local humanoid = character:WaitForChild("Humanoid")
  21. local character_descendants = character:GetDescendants()
  22. local body_parts = {}
  23.  
  24. local default_walk_speed = humanoid.WalkSpeed
  25.  
  26. local swim_animation = script:WaitForChild("Swim")
  27. local idle_animation = script:WaitForChild("Idle")
  28.  
  29. local loaded_swim_animation = humanoid:LoadAnimation(swim_animation)
  30. local loaded_idle_animation = humanoid:LoadAnimation(idle_animation)
  31.  
  32. local y_zero_vector = Vector3.new(1, 0, 1)
  33. local zero_vector = Vector3.new()
  34.  
  35. local body_velocity = Instance.new("BodyVelocity")
  36. body_velocity.Velocity = zero_vector
  37. body_velocity.MaxForce = zero_vector
  38. body_velocity.Parent = root_part
  39.  
  40. local body_position = Instance.new("BodyPosition")
  41. body_position.MaxForce = zero_vector
  42. body_position.D = zero_vector
  43. body_position.Parent = root_part
  44.  
  45. local ball_guard = Instance.new("Part")
  46. ball_guard.Name = "BallGuard"
  47. ball_guard.Shape = Enum.PartType.Ball
  48. ball_guard.Size = Vector3.new(9, 9, 9)
  49. ball_guard.CanCollide = false
  50. ball_guard.Transparency = 1
  51. ball_guard.Massless = true
  52. ball_guard.Position = root_part.Position
  53.  
  54. local weld = Instance.new("Weld")
  55. weld.Name = "BallGuardAttachment"
  56. weld.Part0 = root_part
  57. weld.Part1 = ball_guard
  58. weld.Parent = ball_guard
  59.  
  60. ball_guard.Parent = root_part
  61.  
  62. local body_velocity_on = false
  63. local body_position_on = false
  64.  
  65. local debounce = tick()
  66.  
  67. local collision_connection, collision_ended_connection
  68.  
  69. --[[ Functions ]]--
  70.  
  71. local function collision_handler(hit)
  72. if not body_parts[hit] then
  73. collision_counter = collision_counter + 1
  74. end
  75. end
  76.  
  77. local function collision_ended_handler(part)
  78. if not body_parts[part] then
  79. collision_counter = collision_counter - 1
  80. end
  81. end
  82.  
  83. --[[ Event Listeners ]]--
  84.  
  85. input_service.JumpRequest:Connect(function()
  86. if is_swimming and root_part.Position.Y > (top_height - 1.5) then
  87. humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
  88. end
  89. end)
  90.  
  91. run_service.RenderStepped:Connect(function()
  92.  
  93. local pos = root_part.Position
  94. local y_pos = pos.Y
  95.  
  96. if y_pos > base_height and y_pos < top_height then
  97.  
  98. if not is_swimming then
  99. humanoid.WalkSpeed = 0
  100. ball_guard.CanCollide = true
  101. collision_connection = ball_guard.Touched:Connect(collision_handler)
  102. collision_ended_connection = ball_guard.TouchEnded:Connect(collision_ended_handler)
  103. end
  104.  
  105. is_swimming = true
  106.  
  107. local move_vector = humanoid.MoveDirection
  108. local y_zero_move_vector = (move_vector*y_zero_vector).unit
  109. local y_zero_look_vector = (camera.CFrame.LookVector*y_zero_vector).unit
  110.  
  111. if move_vector == zero_vector then
  112.  
  113. if platform_stand_enabled then
  114. humanoid.PlatformStand = false
  115. platform_stand_enabled = false
  116. end
  117.  
  118. local direction = camera.CFrame.LookVector
  119. local position = root_part.Position
  120.  
  121. root_part.CFrame = CFrame.new(position)*CFrame.Angles(0, math.atan2(-direction.X, -direction.Z), 0)
  122.  
  123. if body_velocity_on then
  124. body_velocity.Velocity = zero_vector
  125. body_velocity.MaxForce = zero_vector
  126. print("oh yeah")
  127. loaded_swim_animation:Stop()
  128. body_velocity_on = false
  129. end
  130.  
  131. body_position.Position = Vector3.new(pos.X, top_height + 1, pos.Z)
  132.  
  133. if not body_position_on then
  134. body_position.MaxForce = Vector3.new(0, 7250, 0)
  135. body_position.D = 3000
  136. print("uh, what's going on?")
  137. loaded_idle_animation:Play()
  138. body_position_on = true
  139. end
  140.  
  141. elseif y_zero_move_vector:Dot(y_zero_look_vector) > 0.5 then -- something to check against here
  142.  
  143. if collision_counter == 0 then
  144. if not platform_stand_enabled then
  145. humanoid.PlatformStand = true
  146. platform_stand_enabled = true
  147. end
  148. else
  149. if platform_stand_enabled then
  150. humanoid.PlatformStand = false
  151. platform_stand_enabled = false
  152. end
  153. end
  154.  
  155. local direction = camera.CFrame.LookVector
  156. local position = root_part.Position
  157.  
  158. root_part.CFrame = CFrame.new(position, position + direction)*CFrame.Angles(math.pi/2, math.pi, math.pi)
  159. body_velocity.Velocity = camera.CFrame.LookVector*30
  160.  
  161. if body_position_on then
  162.  
  163. body_position.MaxForce = zero_vector
  164. body_position.D = zero_vector
  165. print("doing this")
  166. loaded_idle_animation:Stop()
  167. body_position_on = false
  168.  
  169. end
  170.  
  171. if not body_velocity_on then
  172. body_velocity.MaxForce = Vector3.new(1, 1, 1)*math.huge
  173. loaded_swim_animation:Play()
  174. body_velocity_on = true
  175. end
  176.  
  177. else
  178.  
  179. if platform_stand_enabled then
  180. humanoid.PlatformStand = false
  181. platform_stand_enabled = false
  182. end
  183.  
  184. local direction = camera.CFrame.LookVector
  185. local position = root_part.Position
  186.  
  187. root_part.CFrame = CFrame.new(position)*CFrame.Angles(0, math.atan2(-direction.X, -direction.Z), 0)
  188.  
  189. if body_velocity_on then
  190.  
  191. loaded_swim_animation:Stop()
  192.  
  193. body_velocity.Velocity = zero_vector
  194. body_velocity.MaxForce = zero_vector
  195.  
  196. body_velocity_on = false
  197.  
  198. end
  199.  
  200. body_position.Position = Vector3.new(pos.X, top_height + 1, pos.Z)
  201. if not body_position_on then
  202. body_position.MaxForce = Vector3.new(0, 7250, 0)
  203. body_position.D = 3000
  204. loaded_idle_animation:Play()
  205. body_position_on = false
  206. end
  207. end
  208.  
  209. elseif is_swimming then
  210.  
  211. ball_guard.CanCollide = false
  212. humanoid.PlatformStand = false
  213.  
  214. if collision_connection then
  215. collision_connection:Disconnect()
  216. end
  217.  
  218. if collision_ended_connection then
  219. collision_ended_connection:Disconnect()
  220. end
  221.  
  222. collision_counter = 0
  223.  
  224. if body_position_on then
  225. body_position.MaxForce = zero_vector
  226. body_position.D = zero_vector
  227. body_position_on = false
  228. elseif body_velocity_on then
  229. body_velocity.Velocity = zero_vector
  230. body_velocity.MaxForce = zero_vector
  231. body_velocity_on = false
  232. end
  233.  
  234. if loaded_idle_animation.IsPlaying then
  235. print("yeah")
  236. loaded_idle_animation:Stop()
  237. elseif loaded_swim_animation.IsPlaying then
  238. loaded_swim_animation:Stop()
  239. end
  240.  
  241. humanoid.WalkSpeed = default_walk_speed
  242. is_swimming = false
  243. end
  244. end)
  245.  
  246. for _, part in ipairs(character_descendants) do
  247. body_parts[part] = true
  248. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement