Disable gravity for a specific part?

I’m trying to make the red cube bounce around the box with black borders kinda like the DVD logo. It works if the part is flat, but because the part is at an angle, gravity instead messes with the cube and pushes it to the bottom.

I tried using a vector force, but I couldn’t get it to work, probably because I don’t understand them real well.

Code
local part = script.Parent
local random = Random.new()
local speed = random:NextNumber(24, 36)

part.CFrame = part.Parent.PrimaryPart.CFrame * CFrame.new(random:NextInteger(-14, 14), 4, random:NextInteger(-44, 44))

wait(1)

-- Ensure that the speed of this part is actually equal to speed
local speed2 = speed * speed -- Speed squared
local xvelocity = random:NextNumber(100, speed2 - 25)
local zvelocity = speed2 - xvelocity
part.AssemblyLinearVelocity = Vector3.new(math.sqrt(xvelocity), 0, math.sqrt(zvelocity))

part.Touched:Connect(function(border)
	if border.Name == "Z-Axis" then
		part.AssemblyLinearVelocity = part.AssemblyLinearVelocity * Vector3.new(-1, 1, 1)
	elseif border.name == "X-Axis" then
		part.AssemblyLinearVelocity = part.AssemblyLinearVelocity * Vector3.new(1, 1, -1)
	end
end)

Try using bodyvelocity.

I recommend turn on massless bool in part properties but i don’t guarantee for perfect work.

Since gravity is a constant acceleration down, you would oppose the gravity force that is found in the properties in Workspace.

https://developer.roblox.com/en-us/api-reference/property/Workspace/Gravity

Set a VectorForce with the Y value as the Workspace.Gravity, or whatever that works.

Multiply gravity by the assembly mass and set the force to that^

3 Likes

This is the code that runs the red cube, btw:

local part = script.Parent
local random = Random.new()
local speed = random:NextNumber(24, 36)

part.CFrame = part.Parent.PrimaryPart.CFrame * CFrame.new(random:NextInteger(-14, 14), 4, random:NextInteger(-44, 44))

wait(1)

-- Ensure that the speed of this part is actually equal to speed
local speed2 = speed * speed -- Speed squared
local xvelocity = random:NextNumber(100, speed2 - 25)
local zvelocity = speed2 - xvelocity
part.AssemblyLinearVelocity = Vector3.new(math.sqrt(xvelocity), 0, math.sqrt(zvelocity))

part.Touched:Connect(function(border)
	if border.Name == "Z-Axis" then
		part.AssemblyLinearVelocity = part.AssemblyLinearVelocity * Vector3.new(-1, 1, 1)
	elseif border.name == "X-Axis" then
		part.AssemblyLinearVelocity = part.AssemblyLinearVelocity * Vector3.new(1, 1, -1)
	end
end)

The to my knowledge easiest method is to insert a BodyForce into the part you want to be weightless. VectorForces are preferred but I too don’t appreciate the additional bloat of one more attachment.

If gravity and the part’s size (mass) is constant, do this one-time for each part you want weightless:

local bodyForce = Instance.new("BodyForce")
bodyForce.Force = Vector3.new(0, part:GetMass()*workspace.Gravity, 0)
bodyForce.Parent = part

If gravity or the part changes you can just repeat the second line within a RunService.Stepped event listener.

1 Like

Just like with the other solutions, the problem is the same. The part flies away instead of keeping the part attached to the base

You can try making the borders taller, and for viewing purposes make them invisible and re-add your current lines with a different name, cancollide cantouch etc disabled

You’ll need to cancel out the horizonal component of the normal force.

If you do that, the part will “fall” onto the board, but the gravity/normal-force won’t move it sideways. Completely canceling the gravity force will make the block float, but it needs to stay on the board, so that’s not a good solution.

Edit:
Steps:

  1. Calc horizontal component
  2. Create a BodyForce and set the force to the opposite of the horizontal component

To put this into code

Find this vector (unit vector, probably lookvector, -lookvector, rightvector etc)
image
And set the force equal to vector * vector:Dot(mass*gravity)

3 Likes