So, Im trying to make a code that creates a bridge type structure with even gaps in-between the player and the object, anybody know I should start with when creating this code?
You can fetch the two opposing faces of a part, using LookVector (I can go into explanation on this further if you’d like). Once you have the two CFrames, you can just divide and iterate in the desired axis.
CFrame:Lerp() has you covered.
I’ve thought about constantly dividing, but wouldn’t I be limited to how many times I’ve divided? Lets say I only divide the two object positions by 4, then If the objects were too far, the bridge parts would be too far away from each to jump, no?
This will make a thin part on a random position but does the right size
local function CreateBridge(Part1, Part2)
local Bridge = Instance.new("Part", workspace)
local EstimatedBridgeSize = Part1.Position - Part2.Position
Bridge.Anchored = true
Bridge.Size = EstimatedBridgeSize
end
CreateBridge(script.Parent.lol, script.Parent.lol2)
you can use lerp(). and change the easing styles. different easing styles will make different results.
as for the size you can check the magnitude.
local p1,p2 = workspace.Part.CFrame,workspace.Part2.CFrame
local gaps = 5
local model = Instance.new("Model",workspace)
local dist = (p1.Position - p2.Position).Magnitude
local partCount = 15
for i = 0,partCount do
local part = Instance.new("Part",model)
part.Size = Vector3.new(dist/partCount - gaps,0.5,5)
part.CFrame = p1:Lerp(p2,i/partCount)
end
this one is a basic linear.
below are easing algorithm taken from Easing Functions Cheat Sheet that are translated to roblox lua.
Easing Algorithm
local function lerp(a,b,t)
return a + (b-a) * t
end
local function linearin(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = x
table.insert(tab,lerp(a,b,t))
task.wait()
end
return tab
end
--[[ QUADS ]]
local function quadin(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = x * x
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function quadout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = 1 - (1 - x) * (1 - x)
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function quadinout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t
if x < 0.5 then
t = 2 * x * x
else
t = 1 - math.pow(-2 * x + 2, 2) / 2
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
--[[ SINE ]]
local function sinein(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = 1 - math.cos((x * math.pi) / 2)
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function sineout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = math.sin((x * math.pi) / 2)
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function sineinout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = -(math.cos(math.pi * x) - 1) / 2
table.insert(tab,lerp(a,b,t))
end
return tab
end
--[[ QUINT ]]
local function quintin(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = x * x * x * x * x
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function quintout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = 1 - math.pow(1 - x, 5)
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function quintinout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t
if x < 0.5 then
t = 16 * x * x * x * x * x
else
t = 1 - math.pow(-2 * x + 2, 5) / 2
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
--[[ Exponential ]]
local function expoin(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t
if x == 0 then
t = 0
else
t = math.pow(2, 10 * x - 10)
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function expoout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t
if x == 1 then
t = 1
else
t = 1 - math.pow(2, -10 * x)
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function expoinout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t
if x == 1 then
t = 1
elseif x == 0 then
t = 0
elseif x < 0.5 then
t = math.pow(2, 20 * x - 10) / 2
else
t = (2 - math.pow(2, -20 * x + 10)) / 2
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
--[[ Circle ]]
local function circin(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = 1 - math.sqrt(1 - math.pow(x, 2))
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function circout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = math.sqrt(1 - math.pow(x - 1, 2))
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function circinout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t
if x < 0.5 then
t = (1 - math.sqrt(1 - math.pow(2 * x, 2))) / 2
else
t = (math.sqrt(1 - math.pow(-2 * x + 2, 2)) + 1) / 2
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
--[[ Back ]]
local function backin(a,b,totalframe)
local tab = {}
local c1,c3 = 1.70158,1 + 1.70158
for i = 1,totalframe do
x = i/totalframe
local t = c3 * x * x * x - c1 * x * x
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function backout(a,b,totalframe)
local tab = {}
local c1,c3 = 1.70158,1 + 1.70158
for i = 1,totalframe do
x = i/totalframe
local t = 1 + c3 * math.pow(x - 1, 3) + c1 * math.pow(x - 1, 2)
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function backinout(a,b,totalframe)
local tab = {}
local c1,c2 = 1.70158,1.525 * 1.70158
for i = 1,totalframe do
x = i/totalframe
local t
if x < 0.5 then
t = (math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2
else
t = (math.pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
--[[ Elastic ]]
local function elasticin(a,b,totalframe)
local tab = {}
local c4 = (2 * math.pi) / 3
for i = 1,totalframe do
x = i/totalframe
local t
if x == 0 then
t = 0
elseif x == 1 then
t = 1
else
t = -math.pow(2, 10 * x - 10) * math.sin((x * 10 - 10.75) * c4)
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function elasticout(a,b,totalframe)
local tab = {}
local c4 = (2 * math.pi) / 3
for i = 1,totalframe do
x = i/totalframe
local t
if x == 0 then
t = 0
elseif x == 1 then
t = 1
else
t = math.pow(2, -10 * x) * math.sin((x * 10 - 0.75) * c4) + 1
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function elasticinout(a,b,totalframe)
local tab = {}
local c5 = (2 * math.pi) / 4.5
for i = 1,totalframe do
x = i/totalframe
local t
if x == 0 then
t = 0
elseif x == 1 then
t = 1
elseif x < 0.5 then
t = -(math.pow(2, 20 * x - 10) * math.sin((20 * x - 11.125) * c5)) / 2
else
t = (math.pow(2, -20 * x + 10) * math.sin((20 * x - 11.125) * c5)) / 2 + 1
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
--[[ Bounce ]]
local function outbouncecalc(x)
local n1,d1 = 7.5625,2.75
if x < 1 / d1 then
return n1 * x * x
elseif x < 2 / d1 then
local x2 = x - 1.5
return n1 * (x2 / d1) * x + 0.75
elseif x < 2.5 / d1 then
local x2 = x - 2.25
return n1 * (x2 / d1) * x + 0.9375
else
local x2 = x - 2.625
return n1 * (x2 / d1) * x + 0.984375
end
end
local function bouncein(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t = 1 - outbouncecalc(1 - x)
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function bounceout(a,b,totalframe)
local tab = {}
local n1,d1 = 7.5625,2.75
for i = 1,totalframe do
x = i/totalframe
local t
if x < 1 / d1 then
t = n1 * x * x
elseif x < 2 / d1 then
local x2 = x - 1.5
t = n1 * (x2 / d1) * x + 0.75
elseif x < 2.5 / d1 then
local x2 = x - 2.25
t = n1 * (x2 / d1) * x + 0.9375
else
local x2 = x - 2.625
t = n1 * (x2 / d1) * x + 0.984375
end
table.insert(tab,lerp(a,b,t))
end
return tab
end
local function bounceinout(a,b,totalframe)
local tab = {}
for i = 1,totalframe do
x = i/totalframe
local t
if x < 0.5 then
t = (1 - outbouncecalc(1 - 2 * x)) / 2
else
t = (1 + outbouncecalc(2 * x - 1)) / 2
end
table.insert(totalframe,t)
end
return tab
end