So im making a basketball game and i wanna add physics to my rim. For example like a little wobble effect when dunking or when the ball bounces off the rim.
Ive tried using hinge constrains and a TorsionSpringConstraint, but cant get it working
Id also like for it to move it all directions
heres a example from Practical Basketball
I want it exactly or as close to as this
i made a Module for this and i feel like im close but it dosnt look good.
local RimBend = {}
RimBend.__index = RimBend
local RunService = game:GetService(“RunService”)
local TweenService = game:GetService(“TweenService”)
local RADIUS = 3
local BEND_POWER = 0.25
local MAX_ANGLE = math.rad(40)
local IMPACT_THRESHOLD = 0
local BEND_TIME = 0.4
local RETURN_TIME = 0.2
function RimBend.new(rimPart)
local self = setmetatable({}, RimBend)
self.Rim = rimPart
self.OriginalCFrame = rimPart.CFrame
self.IsBent = false
self.InContact = false
self.Cooldown = false
return self
end
function RimBend:ApplyBend(dir, vel)
self.IsBent = true
local angle = math.clamp(vel * BEND_POWER, 0, MAX_ANGLE)
local axis = dir:Cross(Vector3.new(0,1,0))
local bentCFrame = self.OriginalCFrame * CFrame.fromAxisAngle(axis, angle)
local bendTween = TweenService:Create(
self.Rim,
TweenInfo.new(BEND_TIME, Enum.EasingStyle.Quad),
{CFrame = bentCFrame}
)
bendTween:Play()
bendTween.Completed:Once(function()
local returnTween = TweenService:Create(
self.Rim,
TweenInfo.new(RETURN_TIME, Enum.EasingStyle.Back),
{CFrame = self.OriginalCFrame}
)
returnTween:Play()
returnTween.Completed:Once(function()
self.IsBent = false
end)
end)
end
function RimBend:Start(parts)
RunService.Heartbeat:Connect(function()
local touchingSomething = false
for _, part in ipairs(parts) do
if part and part:IsA("BasePart") then
local dist = (part.Position - self.Rim.Position).Magnitude
if dist <= RADIUS then
touchingSomething = true
if not self.IsBent then
local vel = part.AssemblyLinearVelocity.Magnitude
if vel >= IMPACT_THRESHOLD then
local dir = (part.Position - self.Rim.Position).Unit
self:ApplyBend(dir, vel)
end
end
end
end
end
if self.InContact and not touchingSomething then
self.IsBent = false
end
self.InContact = touchingSomething
end)
end
return RimBend
. This is what it looks like.