How to Detect Water Collisions with Custom Rig

Hello all! I had a particular question about how to detect water collisions for custom swimming idle, and motion animations. My rig currently does not “detect” when it is swimming, which I find unusual. I was wondering if there was any other way to be able to go about this, or if I could fix anything to my rig. Thank you all.
Code:

local humanoid = script.Parent:WaitForChild("Humanoid", 2)

local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart", 2)

local animator = humanoid:WaitForChild("Animator", 2)

local idleAnimation = nil

local runAnimation = nil

local swimAnimationIdle = nil

local swimAnimation = nil

local checkTrue = false

if game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(script.Parent.Name) then

	local fetNameCut = script.Parent.Name

	local originalName = script.Parent.Name

	if string.find( script.Parent.Name,"Lustrous") ~= nil then
		for i, v in pairs(string.split(script.Parent.Name,"Lustrous")) do
			if v ~= nil then
				fetNameCut = v
			end
		end
	end

	idleAnimation = animator:LoadAnimation(game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(fetNameCut).Idle)

	runAnimation = animator:LoadAnimation(game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(fetNameCut).Running)

	if game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(fetNameCut):FindFirstChild("SwimmingIdle") then
		if game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(fetNameCut):FindFirstChild("SwimmingIdle").AnimationId ~= "" or nil then
			swimAnimationIdle = animator:LoadAnimation(game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(fetNameCut).SwimmingIdle)
		end
	end
	
	if game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(fetNameCut):FindFirstChild("SwimmingSwim") then
		if game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(fetNameCut):FindFirstChild("SwimmingSwim").AnimationId ~= "" or nil then
			swimAnimation = animator:LoadAnimation(game.ReplicatedStorage.FeteriansAnimations:FindFirstChild(fetNameCut).SwimmingSwim)
		end
	end

	humanoid.Running:Connect(function(speed)	
		
		if script.Parent.Humanoid.FloorMaterial ~= Enum.Material.Air then
			if speed > 0 then
				if humanoid.WalkSpeed ~= 0 then
					idleAnimation:Stop()

					if swimAnimation ~= nil then
						swimAnimation:Stop()
					end
					
					if swimAnimationIdle ~= nil then
						swimAnimationIdle:Stop()
					end

					runAnimation:Play()
				elseif humanoid.WalkSpeed <= 0 then
					runAnimation:Stop()

					if swimAnimation ~= nil then
						swimAnimation:Stop()
					end
					
					if swimAnimationIdle ~= nil then
						swimAnimationIdle:Stop()
					end

					idleAnimation:Stop()
				end
			else
				if humanoid.WalkSpeed ~= 0 then
					runAnimation:Stop()	

					if swimAnimation ~= nil then
						swimAnimation:Stop()
					end				
					
					if swimAnimationIdle ~= nil then
						swimAnimationIdle:Stop()
					end

					idleAnimation:Play()

					repeat
						if script.Parent:GetAttribute("Activity") == false then
							checkTrue = false

							for i = 1, 5, 1 do
								task.wait(1)

								if speed > 0 then
									checkTrue = true

									break
								end

								if script.Parent:GetAttribute("Activity") == true then
									break
								end
							end

							if checkTrue == true then						
								break
							end

							if script.Parent:GetAttribute("Activity") == true then
								break
							end

							if script.Parent:GetAttribute("Activity") == false then

								if game.ReplicatedStorage.FeteriansAnimations.Activities.Sleeping:FindFirstChild("Sleeping"..script.Parent.Name) then								
									for i, v in pairs(script.Parent:GetDescendants()) do
										if v:IsA("MeshPart") then										
											v.TextureID = game.ReplicatedStorage.FeteriansAnimations.Activities.Sleeping:FindFirstChild("Sleeping"..script.Parent.Name).Value
										end
									end
								end

								task.wait(0.25)

								if string.find(script.Parent.Name,"Lustrous") ~= nil then									
									for i, v in pairs(script.Parent:GetDescendants()) do
										if v:IsA("MeshPart") then
											v.TextureID = game.ReplicatedStorage.LustrousTextures:FindFirstChild("Lustrous"..fetNameCut).Value
										end
									end
								else
									for i, v in pairs(game.ReplicatedStorage.FeteriansModels:FindFirstChild(fetNameCut):GetChildren()) do
										if v:IsA("MeshPart") then
											for index, value in pairs(script.Parent:GetDescendants()) do
												if value:IsA("MeshPart") then
													value.TextureID = v.TextureID
												end
											end
										end
									end
								end
							end
						end

						if script.Parent:GetAttribute("Activity") == true then
							if script.Parent:GetAttribute("ActivityType") == "Sleeping" then
								if game.ReplicatedStorage.FeteriansAnimations.Activities.Sleeping:FindFirstChild("Sleeping"..originalName) then								
									for i, v in pairs(script.Parent:GetDescendants()) do
										if v:IsA("MeshPart") then										
											if v.TextureID ~= game.ReplicatedStorage.FeteriansAnimations.Activities.Sleeping:FindFirstChild("Sleeping"..originalName).Value then
												v.TextureID = game.ReplicatedStorage.FeteriansAnimations.Activities.Sleeping:FindFirstChild("Sleeping"..originalName).Value
											end
										end
									end
								end
							end
						end

						task.wait(0.1)

					until speed > 0

				elseif humanoid.WalkSpeed <= 0 then
					runAnimation:Stop()
					idleAnimation:Stop()
				end	
			end
		else	
			if swimAnimation ~= nil then
				runAnimation:Stop()
				idleAnimation:Stop()
				
				swimAnimation:Play()
			end
		end
	end)
	
end

Pictures:
image
image
image

You could do this with raycasting just cast a ray from characters position or a little above down and see if it hits terrain water if so then they should be swimming you may need to tweak it a bit on distance of the ray etc

1 Like

Thank you so much for your help, it is greatly appreciated!!

Where this script should be parented at?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.