I have a very simple code for a custom swim system (for a custom character). What it does is sets a BodyForce on the character, which goes toward the LookVector of the player’s camera (the player is in first person). This system is designed to work on Roblox Terrain Water, and it does work as intended. However, I was wondering, how do I let the player dive into the water (when looking down), and rise (when looking up). Right now, it just floats on top of the water, and moves forward, left, and right, even when looking straight down. My code (local script inside the character):
local Camera = workspace.CurrentCamera while wait() do local Direction = Vector3.new(Camera.CFrame.LookVector.X,0,Camera.CFrame.LookVector.Z)*10000 --game.Players.LocalPlayer:Move(Vector3.new(0, 0, -1), true) if not game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward") then local bodyForce = Instance.new("BodyForce") bodyForce.Name = "Forward" bodyForce.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward").Force = Direction else game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward").Force = Direction end end
To repeat my question - how would I modify this code, so the player can dive down into the water, rise, AND go left and right? I am an intermediate-level scripter, but I am seriously stuck on this one. Thank you!
How do I tell if the player is looking up or down, and apply a bodyforce depending on whether it is up or down? I have this code so far
function UpOrDown(LookVector) local LookVector = LookVector.Unit local UpVector = Vector3.new(0, 1, 0) --Should return a value within range [-1, 1] return LookVector:Dot(UpVector) end
But how would I use the results (in an if statement)? Thanks!