Hello. I have this script that basically makes parts bounce off walls, like a ping pong ball. (It’s inspired by horse race test btw.)
I’ve used the Linear Velocity constraint for the parts to move constantly. But the thing is that the parts don’t bounce always in the right way. I put a random multiplier since the movement was too flat.
How should I change the line direction velocity formula for it to be more accurate?
local function bounce(part,lineVelocity,hitSound)
part.Touched:Connect(function(partThatHit)
if partThatHit.Name == "wall" then
hitSound.PlaybackSpeed = math.random(0.5,2)
hitSound:Play()
local rand1 = math.random(1,2)
local multiplier
local sum = (partThatHit.CFrame.LookVector.Unit + part.CFrame.LookVector.Unit) *Vector3.new(1,0,1)
--random multiplier
if rand1 == 1 then
multiplier = math.random(1,10)
elseif rand1 == 2 then
multiplier = math.random(-10,-1)
end
lineVelocity.LineDirection = sum + Vector3.new(multiplier,0,multiplier).Unit
part.Timer.Value = 10
elseif partThatHit.Name == "horse" then
hitSound.PlaybackSpeed = math.random(0.5,2)
hitSound:Play()
lineVelocity.LineDirection = -lineVelocity.LineDirection.Unit
part.Timer.Value = 10
end
end)
end