hello developers! im creating a first person shooter and am creating sine wave to create the walking animations for the viewmodels. this looks really good and takes off a lot of work, but the transition between the idle waves and the walking waves is instant. is there a way for me to lerp or tween the two waves to get a smooth transition? if so, it would be great help. thank you!
here is my code that handles the waves
local bobx
local boby
if walking then
bobx = math.sin(time() * 6)/20
boby = math.cos(time() * 12)/20
elseif idle then
bobx = math.sin(time() * 1.5)/50
boby = math.cos(time() * 3)/50
elseif inAir then
bobx = 0
boby = 0
end
local finalbob = CFrame.new(bobx, boby, 0)
currentVM:SetPrimaryPartCFrame(camera.CFrame * sway * finalbob)
so basically you need a way to dampen the sway from when idle to walking
i am thinking about something liike this
local bobx
local boby
local steps = 20
local lastState
--[[
/////////////////
state
0 - walking
1 - idle
2 - inAir
/////////////////
--]]
--[[
-- walking
local wbobx = math.sin(time() * 6)/20
local wboby = math.cos(time() * 12)/20
--idle
local ibobx = math.sin(time() * 1.5)/50
local iboby = math.cos(time() * 3)/50
--inAir
local abobx = 0
local aboby = 0
--]]
local bobxarr = [wbobx, ibobox, abobx]
local bobyarr = [wboby, iboboy, aboby]
if state ~= lastState
lastState = state
-- we will be using weighted values for the transition
currentStep = 0
--idented part would be in some form of loop
bobx = bobxarr[lastState]*1-currentStep+bobxarr[state]*currentStep
boby = bobyarr[lastState]*1-currentStep+bobyarr[state]*currentStep
currentstep += 1/steps
local finalbob = CFrame.new(bobx, boby, 0)
currentVM:SetPrimaryPartCFrame(camera.CFrame * sway * finalbob)
so basically you would want to do some tween between the states manually
a tween is going through a series of values over a period of time
for us the tween is between 0 and 1 in 20 steps
how fast that is depends on how much time it elapses before currentstep += 1/steps is called again
the weighted values part
think about it as how much influence one type of bob has over the final bob
let’s say current step is 0.3 we were idle and now we’re walking
the final bob would be 70% idlebob + 30% walkingbob
or 0.3 * idlebob + 0.3 * walkingbob
I handle this by using the input for the functions as time that the player has been walking (gets reset if they stop walking). You will also have to ensure that these functions resolve to 0 when the elapsed time the players have walked is 0.