How can I fully cancel out gravity for a player?

Heya! So, I am working on a custom water physics engine, but I’ve run into a problem. Basically, I seem to not be able to get precise enough mass measurements for the player to fully cancel out gravity. Is there any way to completely stop the player from sinking without changing the workspace gravity, or do I just have to live with the player slowly sinking?

You can see that the player starts from the spawnpoint, and there was no force pushing them downwards, but they are still sinking.
Code:

task.wait()
local RS = game:GetService("RunService")
local character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
local water = game.Workspace.Water
local hrp = character:WaitForChild("HumanoidRootPart")
local function getPlayerMass()
	local playerMass = 0
	for i, v in pairs(character:GetDescendants()) do
		task.wait()
		if v:IsA("BasePart") or v:IsA("MeshPart") == true then
			if not v.Massless then
				playerMass += v.Mass
			end
		end
	end
	return playerMass
end
print(getPlayerMass())
local playerMass = getPlayerMass()
local att = Instance.new("Attachment")
att.WorldPosition = hrp.Position
att.Name = ("SwimmingATT")
att.Parent = hrp

local function antiGrav()
	local force = Instance.new("VectorForce")
	force.RelativeTo = Enum.ActuatorRelativeTo.World
	force.Force = Vector3.new(0, workspace.Gravity * playerMass, 0)
	force.ApplyAtCenterOfMass = true
	force.Attachment0 = att
	force.Name = ("SwimmingForce")
	force.Parent = hrp
	force.Enabled = false --(For now)
	RS.Heartbeat:Connect(function()
		force.Enabled = true
	end)
end

task.wait(1)
print("Triggering func")
antiGrav()

The VectorForce is not doing anything since playerMass is 0, and since you are multiplying with 0, the result will be 0

You can get the character’s mass using hrp.AssemblyMass. If you use gravity * mass for the upwards force the character shouldn’t sink.

I would guess that the problem is that when you calculate the mass initially that maybe some of the parts aren’t loaded, so the mass isn’t accurate. To combat this, I’d recommend using hrp.AssemblyMass instead of playerMass here:

force.Force = Vector3.new(0, workspace.Gravity * playerMass, 0)

so:

force.Force = Vector3.new(0, workspace.Gravity * hrp.AssemblyMass, 0)

Doing this still makes the player sink, but it seems alot less noticeable, so this seems to work. Thanks for the help!

1 Like

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