Help with Velocity stuff

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

use custom phisical propertys so no friction and remove velocity loss and add bounce on a part. remember use a part that’s unanchored after do do same thing with the other parts that the “horse” is colliding with. make sure the colliding parts are anchored. remember to make the racetrack lay flat on the ground to start the race add velocity then delete. Doing all of this makes everything simple with no code. but if you want it to math go ahead and ping me

1 Like