I'm currently in the process of creating a 3D Connect Four Game. Basically, I need to make the pegs "bounce" when they fall.
I have the basics for this. Here's a link to a gif I made of the script working.
However, there's a problem.
Here's a link to the problem.
It looks like the bouncing isn't changing. It goes below the other pegs when they stack too high. I somehow need to account for this.
Here's the function,
local y = 2.75
local mag = 6
local start = peg.CFrame
local finish = CFrame.new(start.x,((y)+value*mag)-mag,start.z)
-- value equals the peg position. If there were 0 pegs in a column, then value would equal 1. Goes up to 6.
count = 0
local function lurp(s,f,num)
count = count+1
for i = 0,1,num do
peg.CFrame = s:lerp(f,i)
wait()
end
if count > 4 then-- Number of lerps. num/2 = bounces
return
elseif count%2 == 0 then-- Down
lurp(CFrame.new(f.x, f.y, f.z),CFrame.new(s.x, s.y, s.z),0.12);
else-- Up
lurp(CFrame.new(f.x, f.y, f.z),CFrame.new(s.x, s.y/2, s.z),0.12);
end
end
lurp(start,finish, 0.08);
The lurp function lerps the peg in a way thay would mimic a bounce. The down part on line 22 seems to be the problem area from what I could tell.
Useful info
Because this is a math function, it might be worth knowing some of the maths involved.
2.75 studs is the bottom of the board or the y position of the last peg hole.
6 studs is the distance between each hole vertical wise, which is all we need to worry about.
