How would I make a perfect anti-gravity force?

So I was working on this player movement script that counteracts gravity so you float midair, the issue is, when you bump into another part, you either go up or down and the effect is ruined.

Sections of my movement script that counteracts gravity:

``````attachment.Position = prim.Position
attachment.Parent = prim

force.Attachment0 = attachment
force.Force = Vector3.new(0, workspace.Gravity * prim:GetMass(), 0)
force.RelativeTo = "Attachment0"
force.ApplyAtCenterOfMass = true
force.Parent = prim
``````

Any help is appreciated!

3 Likes

maybe try adding the velocity of the rootpart into the equation.
Vector3.new(0, (workspace.Gravity * prim:GetMass() )+prim.AssemblyLinearVelocity, 0)

1 Like

So would I just set the velocity of the primary part of the player to 0 every frame? Or every time the velocity changes?

Or is that just a dumb idea?

That idea is not out of the box but are you getting the whole mass of the character or just the rootpart?
I have a function to get the model of the whole mass:

``````local function GetModelMass(Model)
local Mass = 0
for i, v in ipairs(Model:GetDescendants()) do
if v:IsA("BasePart") then
Mass += v:GetMass()
end
end
return Mass
end
``````

The character is just the humanoid root part which is shaped as a cube. I will use the function for other projects though, itâ€™s very nice.

So there are no other parts in the character, but also maybe try making the vectorforce relative to the world and every heartbeat do something like this;

``````RunService.HeartBeat:Connect(function()
force.Force = attachment.Position + Vector3.new(0, workspace.Gravity * prim:GetMass(), 0)
end)
``````
2 Likes

Why not just use a body position with only max force on the y axis

2 Likes

Iâ€™ve tried that before and it didnâ€™t work.

What was the issue with it? With a bodyforce youd have to do like a feedback loop and it might get pretty nasty

I donâ€™t see why that would affect how the character moves.

I am using a vectorforce and itâ€™s working perfectly until I touch another part as I stated before:

I mean the problem with the bodyposition, its most suited for this type of problem

What do you mean this could affect it because relative to world will make it so the Y axis is up and down and not based upon the partâ€™s top and bottom face/cframe.

``````--assuming the parts are unanchored, grouped and welded

local model = script.Parent
local bodyForce = Instance.new("BodyForce", model.PrimaryPart)

local totalMass = 0
for i,v in pairs(model:GetChildren()) do
if v:IsA("BasePart") then
totalMass += v:GetMass()
end
end

bodyForce.Force = Vector3.new(0, totalMass * game.Workspace.Gravity, 0)
``````

The character is not rotating at all, Iâ€™ll show a video clip on how the issue looks:

I suggest using @PapaBreaddâ€™s idea with heartbeat to update the position of the part.

1 Like

There are many ideas such as @PapaBreadd and @ekosi you can test them out to see if either of them works.

I just tested using the body position and the same issue appeared as the first time I used them. The character floats down and it gives a low gravity affect, and the characterâ€™s X and Z axis are both affected.

``````local position = Instance.new("BodyPosition")
position.MaxForce = Vector3.new(1000000, 1000000, 1000000)
position.D = 1000000
position.P = 1000000
position.Parent = prim

coroutine.wrap(function()
game:GetService("RunService").Heartbeat:Connect(function()
position.Position += Vector3.new(0, prim.Position.Y, 0)
end)
end)()
``````
1 Like

That is because there should only be a max force on the y axis, and you should also have the equation in it too; You also need to have less dampening â€śDâ€ť then the Power â€śPâ€ť I prefer around 100 to 10 times less.

``````position.MaxForce = Vector3.new(0, 1, 0)*10^4
position.Position = prim.Position
game:GetService("RunService").Heartbeat:Connect(function()
position.Position = prim.Position+ Vector3.new(0, workspace.Gravity * prim:GetMass(), 0)
end)
``````
3 Likes

Hey sorry for the late response, I had to do something out of the blue, but the code did not work, it had that same low gravity feel that Iâ€™m not after, but the affect was only present in the Y axis, which is good.