Heya! So, I’m working on a custom water system for the player to swim in, I am currently programming the players movement in the water, and I want them to be able to swim in the direction their camera is. My first attempt was to update a variable to match the camera’s CFrame lookvector, but it seems to make the player move extremely slowly in one diagonal line, I’ve tried redoing this implementation multiple times but i end up with a weird result all the time, is there another way to do this?
local lCharacter = game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
local hrp = lCharacter:WaitForChild("HumanoidRootPart")
local checkPart = game.Workspace:WaitForChild("checkPart")
local swimTrigger = game:GetService("ReplicatedStorage").swimTrigger
local hum = lCharacter:WaitForChild("Humanoid")
local UIPS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local camera = game.Workspace.CurrentCamera
local constant = 16
local function humStates(activate, toSet)
hum:SetStateEnabled(Enum.HumanoidStateType.Running, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.GettingUp, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.Freefall, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.FallingDown, activate)
hum:ChangeState(toSet)
end
swimTrigger.OnClientEvent:Connect(function(inWater)
local swimForceX = 0
local swimForceY = 0
local swimForceZ = 0
local inputCounter = 0
local RSconnection
local UIPSbeganconnection
local UIPSendconnection
local linVel
if inWater == true then
humStates(false, Enum.HumanoidStateType.None)
linVel = Instance.new("LinearVelocity")
linVel.Name = ("C.L.V.F")
linVel.MaxForce = math.huge
linVel.Attachment0 = hrp.RootAttachment
linVel.RelativeTo = Enum.ActuatorRelativeTo.World
linVel.VectorVelocity = Vector3.new(0,0,0)
linVel.Enabled = true
linVel.Parent = hrp
UIPSbeganconnection = UIPS.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
inputCounter += 1
if input.KeyCode == Enum.KeyCode.Space then
swimForceY += constant
end
if input.KeyCode == Enum.KeyCode.LeftControl then
swimForceY -= constant
end
if input.KeyCode == Enum.KeyCode.W then
swimForceX = camera.CFrame.LookVector.X * constant
swimForceZ = camera.CFrame.LookVector.Z * constant
end
if input.KeyCode == Enum.KeyCode.S then
end
end)
UIPSendconnection = UIPS.InputEnded:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
inputCounter -= 1
if input.KeyCode == Enum.KeyCode.Space then
swimForceY -= constant
end
if input.KeyCode == Enum.KeyCode.LeftControl then
swimForceY += constant
end
if input.KeyCode == Enum.KeyCode.W then
swimForceX = camera.CFrame.LookVector.X * -constant
swimForceZ = camera.CFrame.LookVector.Z * -constant
end
if input.KeyCode == Enum.KeyCode.S then
end
end)
RSconnection = RS.Heartbeat:Connect(function()
task.wait()
if swimForceY ~= 0 and inputCounter == 0 or inputCounter < 0 then
swimForceY = 0
inputCounter = 0
end
if swimForceY > constant then
swimForceY = constant
elseif swimForceY < -constant then
swimForceY = -constant
end
linVel.VectorVelocity = Vector3.new(swimForceX, swimForceY,swimForceX)
end)
else
humStates(true, Enum.HumanoidStateType.Freefall)
if linVel then
linVel:Destroy()
end
if UIPSbeganconnection and RSconnection and UIPSendconnection then
RSconnection:Disconnect()
UIPSbeganconnection:Disconnect()
UIPSendconnection:Disconnect()
end
end
end)