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)
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.
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:
Calc horizontal component
Create a BodyForce and set the force to the opposite of the horizontal component