I want to simulate the default’s Roblox gravity (don’t ask why) on the player but I’m not sure what I did wrong.
Here this the code:
function getMass(model: Model)
local mass = 0
for i,v in pairs(model:GetDescendants()) do
if(v:IsA("BasePart")) then
mass += v:GetMass()
end
end
return mass
end
function getAssemblyMass(model: Model)
local mass = 0
for i,v in pairs(model:GetDescendants()) do
if(v:IsA("BasePart")) then
mass += v.AssemblyMass
end
end
return mass
end
local gravityForce = Instance.new("VectorForce", rootpart)
gravityForce.Name = "gravityForce"
gravityForce.Attachment0 = rootAttachment
gravityForce.RelativeTo = Enum.ActuatorRelativeTo.World
gravityForce.Force = Vector3.zero
-- this is in a runservice
if not isGrounded() then
gravityForce.Force += getAssemblyMass(char) * (Vector3.yAxis * 21 * -9.81) * deltaTime -- F = ma
else
gravityForce.Force = Vector3.zero
end
What am I doing wrong here? Is it the formula? The equation? Any help is appreciated :,)
I have a VectorVelocity as the VelocityConstraintMode for the X and Z (plane) movement and the Y is 0 so essentially the gravity is non-existent. Only the VectorForce affects the player’s gravity.
I’m not sure how to elaborate on it, but as for the purpose, I want to apply a constant gravity force if the player were to land on a certain surface.
So like you want the players gravity to change depending on the surface they are touching? Have you considered using zones so that when a player is in it, it changes the gravity?
Force is something that should not be applied with +=, it is the change of velocity in time. So it already increases the velocity, you don’t need to increase the force if you don’t want exponential velocity from gravity.
:GetMass() should not be used, prefer .Mass and add not .Anchored and not .Massless to your getMass().
Also don’t fill the second argument of instance.new, parent it in another line.
No, it’s not zero, I do not think that the workspace’s Gravity affects the player as the vector constraint practically cancels out gravity, and I’m just using VectorForce to add the gravity back. I also do not want the workspace gravity to be zero as I plan to add some unanchored parts in the future and I would not like them to float.
Don’t trust the engine. It might change, or fail you. It was causing some memory leak sometime ago. So even vs code extensions warn you about Instance.new having only 1 parameters, but you set 2 etc.