Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- example stuff just trying it out.
- --[[ Services ]]--
- local players = game:GetService("Players")
- local run_service = game:GetService("RunService")
- local input_service = game:GetService("UserInputService")
- local action_service = game:GetService("ContextActionService")
- --[[ Variables ]]--
- local base_height = -50 -- change this in future
- local top_height = 40-- change this in future
- local is_swimming = false
- local platform_stand_enabled = false
- local collision_counter = 0
- local player = players.LocalPlayer
- local camera = workspace.CurrentCamera
- local character = player.Character or player.CharacterAdded:Wait()
- local root_part = character:WaitForChild("HumanoidRootPart")
- local humanoid = character:WaitForChild("Humanoid")
- local character_descendants = character:GetDescendants()
- local body_parts = {}
- local default_walk_speed = humanoid.WalkSpeed
- local swim_animation = script:WaitForChild("Swim")
- local idle_animation = script:WaitForChild("Idle")
- local loaded_swim_animation = humanoid:LoadAnimation(swim_animation)
- local loaded_idle_animation = humanoid:LoadAnimation(idle_animation)
- local y_zero_vector = Vector3.new(1, 0, 1)
- local zero_vector = Vector3.new()
- local body_velocity = Instance.new("BodyVelocity")
- body_velocity.Velocity = zero_vector
- body_velocity.MaxForce = zero_vector
- body_velocity.Parent = root_part
- local body_position = Instance.new("BodyPosition")
- body_position.MaxForce = zero_vector
- body_position.D = zero_vector
- body_position.Parent = root_part
- local ball_guard = Instance.new("Part")
- ball_guard.Name = "BallGuard"
- ball_guard.Shape = Enum.PartType.Ball
- ball_guard.Size = Vector3.new(9, 9, 9)
- ball_guard.CanCollide = false
- ball_guard.Transparency = 1
- ball_guard.Massless = true
- ball_guard.Position = root_part.Position
- local weld = Instance.new("Weld")
- weld.Name = "BallGuardAttachment"
- weld.Part0 = root_part
- weld.Part1 = ball_guard
- weld.Parent = ball_guard
- ball_guard.Parent = root_part
- local body_velocity_on = false
- local body_position_on = false
- local debounce = tick()
- local collision_connection, collision_ended_connection
- --[[ Functions ]]--
- local function collision_handler(hit)
- if not body_parts[hit] then
- collision_counter = collision_counter + 1
- end
- end
- local function collision_ended_handler(part)
- if not body_parts[part] then
- collision_counter = collision_counter - 1
- end
- end
- --[[ Event Listeners ]]--
- input_service.JumpRequest:Connect(function()
- if is_swimming and root_part.Position.Y > (top_height - 1.5) then
- humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
- end
- end)
- run_service.RenderStepped:Connect(function()
- local pos = root_part.Position
- local y_pos = pos.Y
- if y_pos > base_height and y_pos < top_height then
- if not is_swimming then
- humanoid.WalkSpeed = 0
- ball_guard.CanCollide = true
- collision_connection = ball_guard.Touched:Connect(collision_handler)
- collision_ended_connection = ball_guard.TouchEnded:Connect(collision_ended_handler)
- end
- is_swimming = true
- local move_vector = humanoid.MoveDirection
- local y_zero_move_vector = (move_vector*y_zero_vector).unit
- local y_zero_look_vector = (camera.CFrame.LookVector*y_zero_vector).unit
- if move_vector == zero_vector then
- if platform_stand_enabled then
- humanoid.PlatformStand = false
- platform_stand_enabled = false
- end
- local direction = camera.CFrame.LookVector
- local position = root_part.Position
- root_part.CFrame = CFrame.new(position)*CFrame.Angles(0, math.atan2(-direction.X, -direction.Z), 0)
- if body_velocity_on then
- body_velocity.Velocity = zero_vector
- body_velocity.MaxForce = zero_vector
- print("oh yeah")
- loaded_swim_animation:Stop()
- body_velocity_on = false
- end
- body_position.Position = Vector3.new(pos.X, top_height + 1, pos.Z)
- if not body_position_on then
- body_position.MaxForce = Vector3.new(0, 7250, 0)
- body_position.D = 3000
- print("uh, what's going on?")
- loaded_idle_animation:Play()
- body_position_on = true
- end
- elseif y_zero_move_vector:Dot(y_zero_look_vector) > 0.5 then -- something to check against here
- if collision_counter == 0 then
- if not platform_stand_enabled then
- humanoid.PlatformStand = true
- platform_stand_enabled = true
- end
- else
- if platform_stand_enabled then
- humanoid.PlatformStand = false
- platform_stand_enabled = false
- end
- end
- local direction = camera.CFrame.LookVector
- local position = root_part.Position
- root_part.CFrame = CFrame.new(position, position + direction)*CFrame.Angles(math.pi/2, math.pi, math.pi)
- body_velocity.Velocity = camera.CFrame.LookVector*30
- if body_position_on then
- body_position.MaxForce = zero_vector
- body_position.D = zero_vector
- print("doing this")
- loaded_idle_animation:Stop()
- body_position_on = false
- end
- if not body_velocity_on then
- body_velocity.MaxForce = Vector3.new(1, 1, 1)*math.huge
- loaded_swim_animation:Play()
- body_velocity_on = true
- end
- else
- if platform_stand_enabled then
- humanoid.PlatformStand = false
- platform_stand_enabled = false
- end
- local direction = camera.CFrame.LookVector
- local position = root_part.Position
- root_part.CFrame = CFrame.new(position)*CFrame.Angles(0, math.atan2(-direction.X, -direction.Z), 0)
- if body_velocity_on then
- loaded_swim_animation:Stop()
- body_velocity.Velocity = zero_vector
- body_velocity.MaxForce = zero_vector
- body_velocity_on = false
- end
- body_position.Position = Vector3.new(pos.X, top_height + 1, pos.Z)
- if not body_position_on then
- body_position.MaxForce = Vector3.new(0, 7250, 0)
- body_position.D = 3000
- loaded_idle_animation:Play()
- body_position_on = false
- end
- end
- elseif is_swimming then
- ball_guard.CanCollide = false
- humanoid.PlatformStand = false
- if collision_connection then
- collision_connection:Disconnect()
- end
- if collision_ended_connection then
- collision_ended_connection:Disconnect()
- end
- collision_counter = 0
- if body_position_on then
- body_position.MaxForce = zero_vector
- body_position.D = zero_vector
- body_position_on = false
- elseif body_velocity_on then
- body_velocity.Velocity = zero_vector
- body_velocity.MaxForce = zero_vector
- body_velocity_on = false
- end
- if loaded_idle_animation.IsPlaying then
- print("yeah")
- loaded_idle_animation:Stop()
- elseif loaded_swim_animation.IsPlaying then
- loaded_swim_animation:Stop()
- end
- humanoid.WalkSpeed = default_walk_speed
- is_swimming = false
- end
- end)
- for _, part in ipairs(character_descendants) do
- body_parts[part] = true
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement