Advertisement
Kript0

Dynamic Animation Script [Roblox Studio]

Oct 5th, 2023 (edited)
1,180
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.91 KB | Software | 1 0
  1. --[[
  2.     Script modified by Kript0's Proyect
  3.     Original Script from Air Proyect (Daniel)
  4.    
  5.     Delete this message if you want to
  6. ]]--
  7.  
  8. local Character = script.Parent
  9. local Humanoid = Character:WaitForChild("Humanoid")
  10. local pose = "Standing"
  11. local UserInputService = game:GetService("UserInputService")
  12.  
  13. local userNoUpdateOnLoopSuccess, userNoUpdateOnLoopValue = pcall(function() return UserSettings():IsUserFeatureEnabled("UserNoUpdateOnLoop") end)
  14. local userNoUpdateOnLoop = userNoUpdateOnLoopSuccess and userNoUpdateOnLoopValue
  15. local userAnimationSpeedDampeningSuccess, userAnimationSpeedDampeningValue = pcall(function() return UserSettings():IsUserFeatureEnabled("UserAnimationSpeedDampening") end)
  16. local userAnimationSpeedDampening = userAnimationSpeedDampeningSuccess and userAnimationSpeedDampeningValue
  17. local animateScriptEmoteHookFlagExists, animateScriptEmoteHookFlagEnabled = pcall(function()
  18.     return UserSettings():IsUserFeatureEnabled("UserAnimateScriptEmoteHook")
  19. end)
  20. local FFlagAnimateScriptEmoteHook = animateScriptEmoteHookFlagExists and animateScriptEmoteHookFlagEnabled
  21. local AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
  22. local HumanoidHipHeight = 2
  23. local EMOTE_TRANSITION_TIME = 0.1
  24. local currentAnim = ""
  25. local currentAnimInstance = nil
  26. local currentAnimTrack = nil
  27. local currentAnimKeyframeHandler = nil
  28. local currentAnimSpeed = 2
  29. Character:WaitForChild("HumanoidRootPart").Running.Volume = 0
  30. local smallButNotZero = 0.0001
  31. local hrp = Character:WaitForChild("HumanoidRootPart")
  32.  
  33. local runTracks = {
  34.     run = {
  35.         track = nil,
  36.         handler = nil,
  37.         lastWeight = smallButNotZero,
  38.     },
  39.     walk = {
  40.         track = nil,
  41.         handler = nil,
  42.         lastWeight = smallButNotZero,
  43.     },
  44.     back = {
  45.         track = nil,
  46.         handler = nil,
  47.         lastWeight = smallButNotZero,
  48.     },
  49.     left = {
  50.         track = nil,
  51.         handler = nil,
  52.         lastWeight = smallButNotZero,
  53.     },
  54.     right = {
  55.         track = nil,
  56.         handler = nil,
  57.         lastWeight = smallButNotZero,
  58.     },
  59. }
  60. local wasStrafe = false
  61.  
  62. local function doForAllRunTracks(func)
  63.     for name, track in pairs(runTracks) do
  64.         func(track, name)
  65.     end
  66. end
  67.  
  68. local animTable = {}
  69. local animNames = {
  70.     idle =  {
  71.         { id = "http://www.roblox.com/asset/?id=14949780617", weight = 1 },
  72.         { id = "http://www.roblox.com/asset/?id=14949780617", weight = 1 },
  73.         { id = "http://www.roblox.com/asset/?id=14949780617", weight = 9 }
  74.             },
  75.     walk =  {  
  76.         { id = "http://www.roblox.com/asset/?id=14949724983", weight = 10 }
  77.             },
  78.     run =   {
  79.         { id = "http://www.roblox.com/asset/?id=8304552165", weight = 10 }
  80.             },
  81.     back =  {
  82.         { id = "http://www.roblox.com/asset/?id=8500469730", weight = 10 }
  83.             },
  84.     left =  {
  85.         { id = "http://www.roblox.com/asset/?id=8500427567", weight = 10 }
  86.             },
  87.     right =     {
  88.         { id = "http://www.roblox.com/asset/?id=8500441178", weight = 10 }
  89.             },
  90.     swim =  {
  91.                 { id = "http://www.roblox.com/asset/?id=507784897", weight = 10 }
  92.             },
  93.     swimidle =  {
  94.                 { id = "http://www.roblox.com/asset/?id=507785072", weight = 10 }
  95.             },
  96.     jump =  {
  97.             { id = "http://www.roblox.com/asset/?id=8301607806", weight = 10 }
  98.             },
  99.     fall =  {
  100.             { id = "http://www.roblox.com/asset/?id=8301541853", weight = 10 }
  101.             },
  102.     climb = {
  103.                 { id = "http://www.roblox.com/asset/?id=507765644", weight = 10 }
  104.             },
  105.     sit =   {
  106.                 { id = "http://www.roblox.com/asset/?id=2506281703", weight = 10 }
  107.             }, 
  108.     toolnone = {
  109.                 { id = "http://www.roblox.com/asset/?id=507768375", weight = 10 }
  110.             },
  111.     toolslash = {
  112.                 { id = "http://www.roblox.com/asset/?id=522635514", weight = 10 }
  113.             },
  114.     toollunge = {
  115.                 { id = "http://www.roblox.com/asset/?id=522638767", weight = 10 }
  116.             },
  117.     wave = {
  118.                 { id = "http://www.roblox.com/asset/?id=507770239", weight = 10 }
  119.             },
  120.     point = {
  121.                 { id = "http://www.roblox.com/asset/?id=507770453", weight = 10 }
  122.             },
  123.     dance = {
  124.                 { id = "http://www.roblox.com/asset/?id=507771019", weight = 10 },
  125.                 { id = "http://www.roblox.com/asset/?id=507771955", weight = 10 },
  126.                 { id = "http://www.roblox.com/asset/?id=507772104", weight = 10 }
  127.             },
  128.     dance2 = {
  129.                 { id = "http://www.roblox.com/asset/?id=507776043", weight = 10 },
  130.                 { id = "http://www.roblox.com/asset/?id=507776720", weight = 10 },
  131.                 { id = "http://www.roblox.com/asset/?id=507776879", weight = 10 }
  132.             },
  133.     dance3 = {
  134.                 { id = "http://www.roblox.com/asset/?id=507777268", weight = 10 },
  135.                 { id = "http://www.roblox.com/asset/?id=507777451", weight = 10 },
  136.                 { id = "http://www.roblox.com/asset/?id=507777623", weight = 10 }
  137.             },
  138.     laugh = {
  139.                 { id = "http://www.roblox.com/asset/?id=507770818", weight = 10 }
  140.             },
  141.     cheer = {
  142.                 { id = "http://www.roblox.com/asset/?id=507770677", weight = 10 }
  143.             },
  144. }
  145.  
  146.  
  147. local emoteNames = { wave = false, point = false, dance = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  148.  
  149. local PreloadAnimsUserFlag = false
  150. local PreloadedAnims = {}
  151. local successPreloadAnim, msgPreloadAnim = pcall(function()
  152.     PreloadAnimsUserFlag = UserSettings():IsUserFeatureEnabled("UserPreloadAnimations")
  153. end)
  154. if not successPreloadAnim then
  155.     PreloadAnimsUserFlag = false
  156. end
  157.  
  158. math.randomseed(tick())
  159.  
  160. function findExistingAnimationInSet(set, anim)
  161.     if set == nil or anim == nil then
  162.         return 0
  163.     end
  164.    
  165.     for idx = 1, set.count, 1 do
  166.         if set[idx].anim.AnimationId == anim.AnimationId then
  167.             return idx
  168.         end
  169.     end
  170.    
  171.     return 0
  172. end
  173.  
  174. function configureAnimationSet(name, fileList)
  175.     if (animTable[name] ~= nil) then
  176.         for _, connection in pairs(animTable[name].connections) do
  177.             connection:disconnect()
  178.         end
  179.     end
  180.     animTable[name] = {}
  181.     animTable[name].count = 0
  182.     animTable[name].totalWeight = 0
  183.     animTable[name].connections = {}
  184.     local allowCustomAnimations = true
  185.     local success, msg = pcall(function() allowCustomAnimations = game:GetService("StarterPlayer").AllowCustomAnimations end)
  186.     if not success then
  187.         allowCustomAnimations = true
  188.     end
  189.     -- check for config values
  190.     local config = script:FindFirstChild(name)
  191.     if (allowCustomAnimations and config ~= nil) then
  192.         table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  193.         table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  194.        
  195.         local idx = 0
  196.         for _, childPart in pairs(config:GetChildren()) do
  197.             if (childPart:IsA("Animation")) then
  198.                 local newWeight = 1
  199.                 local weightObject = childPart:FindFirstChild("Weight")
  200.                 if (weightObject ~= nil) then
  201.                     newWeight = weightObject.Value
  202.                 end
  203.                 animTable[name].count = animTable[name].count + 1
  204.                 idx = animTable[name].count
  205.                 animTable[name][idx] = {}
  206.                 animTable[name][idx].anim = childPart
  207.                 animTable[name][idx].weight = newWeight
  208.                 animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  209.                 table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  210.                 table.insert(animTable[name].connections, childPart.ChildAdded:connect(function(property) configureAnimationSet(name, fileList) end))
  211.                 table.insert(animTable[name].connections, childPart.ChildRemoved:connect(function(property) configureAnimationSet(name, fileList) end))
  212.             end
  213.         end
  214.     end
  215.    
  216.     -- fallback to defaults
  217.     if (animTable[name].count <= 0) then
  218.         for idx, anim in pairs(fileList) do
  219.             animTable[name][idx] = {}
  220.             animTable[name][idx].anim = Instance.new("Animation")
  221.             animTable[name][idx].anim.Name = name
  222.             animTable[name][idx].anim.AnimationId = anim.id
  223.             animTable[name][idx].weight = anim.weight
  224.             animTable[name].count = animTable[name].count + 1
  225.             animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  226.         end
  227.     end
  228.    
  229.     -- preload anims
  230.     if PreloadAnimsUserFlag then
  231.         for i, animType in pairs(animTable) do
  232.             for idx = 1, animType.count, 1 do
  233.                 if PreloadedAnims[animType[idx].anim.AnimationId] == nil then
  234.                     Humanoid:LoadAnimation(animType[idx].anim)
  235.                     PreloadedAnims[animType[idx].anim.AnimationId] = true
  236.                 end            
  237.             end
  238.         end
  239.     end
  240. end
  241. ------------------------------------------------------------------------------------------------------------
  242. function configureAnimationSetOld(name, fileList)
  243.     if (animTable[name] ~= nil) then
  244.         for _, connection in pairs(animTable[name].connections) do
  245.             connection:disconnect()
  246.         end
  247.     end
  248.     animTable[name] = {}
  249.     animTable[name].count = 0
  250.     animTable[name].totalWeight = 0
  251.     animTable[name].connections = {}
  252.  
  253.     local allowCustomAnimations = true
  254.  
  255.     local success, msg = pcall(function() allowCustomAnimations = game:GetService("StarterPlayer").AllowCustomAnimations end)
  256.     if not success then
  257.         allowCustomAnimations = true
  258.     end
  259.  
  260.     -- check for config values
  261.     local config = script:FindFirstChild(name)
  262.     if (allowCustomAnimations and config ~= nil) then
  263.         table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  264.         table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  265.         local idx = 1
  266.         for _, childPart in pairs(config:GetChildren()) do
  267.             if (childPart:IsA("Animation")) then
  268.                 table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  269.                 animTable[name][idx] = {}
  270.                 animTable[name][idx].anim = childPart
  271.                 local weightObject = childPart:FindFirstChild("Weight")
  272.                 if (weightObject == nil) then
  273.                     animTable[name][idx].weight = 1
  274.                 else
  275.                     animTable[name][idx].weight = weightObject.Value
  276.                 end
  277.                 animTable[name].count = animTable[name].count + 1
  278.                 animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  279.                 idx = idx + 1
  280.             end
  281.         end
  282.     end
  283.  
  284.     if (animTable[name].count <= 0) then
  285.         for idx, anim in pairs(fileList) do
  286.             animTable[name][idx] = {}
  287.             animTable[name][idx].anim = Instance.new("Animation")
  288.             animTable[name][idx].anim.Name = name
  289.             animTable[name][idx].anim.AnimationId = anim.id
  290.             animTable[name][idx].weight = anim.weight
  291.             animTable[name].count = animTable[name].count + 1
  292.             animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  293.         end
  294.     end
  295.  
  296.     if PreloadAnimsUserFlag then
  297.         for i, animType in pairs(animTable) do
  298.             for idx = 1, animType.count, 1 do
  299.                 Humanoid:LoadAnimation(animType[idx].anim)
  300.             end
  301.         end
  302.     end
  303. end
  304.  
  305. -- Setup animation objects
  306. function scriptChildModified(child)
  307.     local fileList = animNames[child.Name]
  308.     if (fileList ~= nil) then
  309.         configureAnimationSet(child.Name, fileList)
  310.     end
  311. end
  312.  
  313. script.ChildAdded:connect(scriptChildModified)
  314. script.ChildRemoved:connect(scriptChildModified)
  315.  
  316. for name, fileList in pairs(animNames) do
  317.     configureAnimationSet(name, fileList)
  318. end
  319.  
  320. -- declarations
  321. local jumpAnimTime = 0
  322. local jumpAnimDuration = 0.2
  323. local toolTransitionTime = 0.1
  324. local fallTransitionTime = 0.2
  325. local currentlyPlayingEmote = false
  326.  
  327. -- functions
  328. function stopAllAnimations()
  329.     local oldAnim = currentAnim
  330.  
  331.     if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  332.         oldAnim = "idle"
  333.     end
  334.    
  335.     if FFlagAnimateScriptEmoteHook and currentlyPlayingEmote then
  336.         oldAnim = "idle"
  337.         currentlyPlayingEmote = false
  338.     end
  339.  
  340.     currentAnim = ""
  341.     currentAnimInstance = nil
  342.     if (currentAnimKeyframeHandler ~= nil) then
  343.         currentAnimKeyframeHandler:disconnect()
  344.     end
  345.  
  346.     if (currentAnimTrack ~= nil) then
  347.         currentAnimTrack:Stop()
  348.         currentAnimTrack:Destroy()
  349.         currentAnimTrack = nil
  350.     end
  351.  
  352.     doForAllRunTracks(function(trackRec, name)
  353.         if trackRec.handler ~= nil then
  354.             trackRec.handler:disconnect()
  355.         end                
  356.     end)
  357.  
  358.     doForAllRunTracks(function(trackRec, name)
  359.         if (trackRec.track ~= nil) then
  360.             trackRec.track:Stop()
  361.             trackRec.track:Destroy()
  362.             trackRec.track = nil
  363.         end    
  364.     end)
  365.    
  366.     return oldAnim
  367. end
  368.  
  369. function getHeightScale()
  370.     if Humanoid then
  371.         if not Humanoid.AutomaticScalingEnabled then
  372.             return 1
  373.         end
  374.         local scale = Humanoid.HipHeight / HumanoidHipHeight
  375.         if userAnimationSpeedDampening then
  376.             if AnimationSpeedDampeningObject == nil then
  377.                 AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
  378.             end
  379.             if AnimationSpeedDampeningObject ~= nil then
  380.                 scale = 1 + (Humanoid.HipHeight - HumanoidHipHeight) * AnimationSpeedDampeningObject.Value / HumanoidHipHeight
  381.             end
  382.         end
  383.         return scale
  384.     end
  385.     return 1
  386. end
  387.  
  388. -------------------- TODO
  389. local upVec = Vector3.new(0, 1, 0)
  390. local blendTime = 0.2
  391.  
  392. function setRunSpeed()
  393.     if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then
  394.         local currWeight = {
  395.             run = smallButNotZero,
  396.             walk = smallButNotZero,
  397.             back = smallButNotZero,
  398.             left = smallButNotZero,
  399.             right = smallButNotZero,
  400.         }
  401.         local camLookVec = workspace.Camera.CFrame.LookVector
  402.         local camLookVecFlat = Vector3.new(camLookVec.x, 0, camLookVec.z).Unit
  403.         local hrpVel = hrp.Velocity
  404.         local hrpVelFlat = Vector3.new(hrpVel.x, 0, hrpVel.z)
  405.         local hrpDir = hrpVelFlat.Unit
  406.         local forwardDot = hrpDir:Dot(camLookVecFlat)
  407.         local rightVec = camLookVecFlat:Cross(upVec)
  408.         local rightDot = hrpDir:Dot(rightVec)  
  409.  
  410.         if Humanoid.WalkSpeed > 7 then
  411.             if forwardDot > 0.1 then
  412.                 currWeight.walk = forwardDot
  413.             elseif forwardDot < -0.1 then
  414.                 currWeight.back = - forwardDot
  415.             end
  416.             if rightDot > 0.1 then
  417.                 currWeight.right = rightDot
  418.             elseif rightDot < -0.1 then
  419.                 currWeight.left = - rightDot
  420.             end
  421.         end
  422.         doForAllRunTracks(function(trackRec, name)
  423.             if currWeight[name] ~= trackRec.lastWeight then
  424.                 trackRec.track:AdjustWeight(currWeight[name], blendTime)
  425.                 trackRec.lastWeight = currWeight[name]                         
  426.             end
  427.         end)       
  428.                
  429.         local lastSpeed = 1
  430.         doForAllRunTracks(function(trackRec, name)
  431.             trackRec.track:AdjustSpeed(lastSpeed, blendTime)
  432.         end)
  433.     end
  434. end
  435.  
  436. function setAnimationSpeed(speed)
  437.     if currentAnim == "walk" then
  438.             setRunSpeed()
  439.     else
  440.         if speed ~= currentAnimSpeed then
  441.             currentAnimSpeed = speed
  442.             currentAnimTrack:AdjustSpeed(1)
  443.         end
  444.     end
  445. end
  446.  
  447. function keyFrameReachedFunc(frameName)
  448.     if (frameName == "End") then
  449.         if currentAnim == "walk" then
  450.             if userNoUpdateOnLoop == true then
  451.                 doForAllRunTracks(function(trackRec)
  452.                     if trackRec.track.Looped ~= true then
  453.                         trackRec.track.TimePosition = 0.0
  454.                     end                
  455.                 end)
  456.                 if currentAnimTrack.Looped ~= true then
  457.                     currentAnimTrack.TimePosition = 0.0
  458.                 end
  459.             else
  460.                 doForAllRunTracks(function(trackRec)
  461.                     if trackRec.track.Looped ~= true then
  462.                         trackRec.track.TimePosition = 0.0
  463.                     end                
  464.                 end)
  465.                 currentAnimTrack.TimePosition = 0.0
  466.             end
  467.         else
  468.             local repeatAnim = currentAnim
  469.             -- return to idle if finishing an emote
  470.             if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  471.                 repeatAnim = "idle"
  472.             end
  473.            
  474.             if FFlagAnimateScriptEmoteHook and currentlyPlayingEmote then
  475.                 if currentAnimTrack.Looped then
  476.                     -- Allow the emote to loop
  477.                     return
  478.                 end
  479.                
  480.                 repeatAnim = "idle"
  481.                 currentlyPlayingEmote = false
  482.             end
  483.            
  484.             local animSpeed = 1
  485.             playAnimation(repeatAnim, 0.2, Humanoid)
  486.             setAnimationSpeed(animSpeed)
  487.         end
  488.     end
  489. end
  490.  
  491. function rollAnimation(animName)
  492.     local roll = math.random(1, animTable[animName].totalWeight)
  493.     local origRoll = roll
  494.     local idx = 1
  495.     while (roll > animTable[animName][idx].weight) do
  496.         roll = roll - animTable[animName][idx].weight
  497.         idx = idx + 1
  498.     end
  499.     return idx
  500. end
  501.  
  502. local function switchToAnim(anim, animName, transitionTime, humanoid)
  503.     -- switch animation    
  504.     if (anim ~= currentAnimInstance) then
  505.        
  506.         if (currentAnimTrack ~= nil) then
  507.             currentAnimTrack:Stop(transitionTime)
  508.             currentAnimTrack:Destroy()
  509.         end
  510.  
  511.         doForAllRunTracks(function(trackRec)
  512.             if (trackRec.track ~= nil) then
  513.                 trackRec.track:Stop(transitionTime)
  514.                 trackRec.track:Destroy()
  515.                 if userNoUpdateOnLoop == true then
  516.                     trackRec.track = nil
  517.                 end
  518.             end
  519.         end)
  520.         currentAnimSpeed = 1.0
  521.    
  522.         -- load it to the humanoid; get AnimationTrack
  523.         currentAnimTrack = humanoid:LoadAnimation(anim)
  524.         currentAnimTrack.Priority = Enum.AnimationPriority.Core
  525.          
  526.         -- play the animation
  527.         currentAnimTrack:Play(transitionTime)
  528.         currentAnim = animName
  529.         currentAnimInstance = anim
  530.  
  531.         -- set up keyframe name triggers
  532.         if (currentAnimKeyframeHandler ~= nil) then
  533.             currentAnimKeyframeHandler:disconnect()
  534.         end
  535.         currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  536.        
  537.         -- check to see if we need to blend a walk/run animation
  538.         if animName == "walk" then
  539.             doForAllRunTracks(function(trackRec, runAnimName)
  540.                 local runIdx = rollAnimation(runAnimName)
  541.                
  542.                 trackRec.track = humanoid:LoadAnimation(animTable[runAnimName][runIdx].anim)
  543.                 trackRec.track.Priority = Enum.AnimationPriority.Core
  544.                 trackRec.track:Play(transitionTime)    
  545.                 trackRec.track:AdjustWeight(smallButNotZero, 0)
  546.                 trackRec.lastWeight = smallButNotZero
  547.            
  548.                 if (trackRec.handler ~= nil) then
  549.                     trackRec.handler:disconnect()
  550.                 end
  551.                 trackRec.handler = trackRec.track.KeyframeReached:connect(keyFrameReachedFunc) 
  552.             end)
  553.         end
  554.     end
  555. end
  556.  
  557. function playAnimation(animName, transitionTime, humanoid)  
  558.     local idx = rollAnimation(animName)
  559.     local anim = animTable[animName][idx].anim
  560.  
  561.     switchToAnim(anim, animName, transitionTime, humanoid)
  562.     currentlyPlayingEmote = false
  563. end
  564.  
  565. function onRunning(speed)  
  566.     if speed > 0 then
  567.         playAnimation("walk", 0.3, Humanoid)
  568.         setAnimationSpeed(1)
  569.         pose = "Running"
  570.     else
  571.         if emoteNames[currentAnim] == nil and not currentlyPlayingEmote then
  572.             playAnimation("idle", 0.2, Humanoid)
  573.             pose = "Standing"
  574.         end
  575.     end
  576. end
  577.  
  578. function onDied()
  579.     pose = "Dead"
  580. end
  581.  
  582. function onJumping()
  583.     playAnimation("jump", 0.2, Humanoid)
  584.     jumpAnimTime = jumpAnimDuration
  585.     pose = "Jumping"
  586. end
  587.  
  588. function onClimbing(speed)
  589.     local scale = 5.0
  590.     playAnimation("climb", 0.1, Humanoid)
  591.     setAnimationSpeed(speed / scale)
  592.     pose = "Climbing"
  593. end
  594.  
  595. function onGettingUp()
  596.     pose = "GettingUp"
  597. end
  598.  
  599. function onFreeFall()
  600.     if (jumpAnimTime <= 0) then
  601.         playAnimation("fall", fallTransitionTime, Humanoid)
  602.     end
  603.     pose = "FreeFall"
  604. end
  605.  
  606. function onFallingDown()
  607.     pose = "FallingDown"
  608. end
  609.  
  610. function onSeated()
  611.     pose = "Seated"
  612. end
  613.  
  614. function onPlatformStanding()
  615.     pose = "PlatformStanding"
  616. end
  617.  
  618. -------------------------------------------------------------------------------------------
  619. -------------------------------------------------------------------------------------------
  620.  
  621. function onSwimming(speed)
  622.     if speed > 1.00 then
  623.         local scale = 10.0
  624.         playAnimation("swim", 0.4, Humanoid)
  625.         setAnimationSpeed(speed / scale)
  626.         pose = "Swimming"
  627.     else
  628.         playAnimation("swimidle", 0.4, Humanoid)
  629.         pose = "Standing"
  630.     end
  631. end
  632.  
  633. function getToolAnim(tool)
  634.     for _, c in ipairs(tool:GetChildren()) do
  635.         if c.Name == "toolanim" and c.className == "StringValue" then
  636.             return c
  637.         end
  638.     end
  639.     return nil
  640. end
  641.  
  642. local lastTick = 0
  643.  
  644. function stepAnimate(currentTime)
  645.     local amplitude = 1
  646.     local frequency = 1
  647.     local deltaTime = currentTime - lastTick
  648.     lastTick = currentTime
  649.  
  650.     local climbFudge = 0
  651.     local setAngles = false
  652.  
  653.     if (jumpAnimTime > 0) then
  654.         jumpAnimTime = jumpAnimTime - deltaTime
  655.     end
  656.  
  657.     if (pose == "FreeFall" and jumpAnimTime <= 0) then
  658.         playAnimation("fall", fallTransitionTime, Humanoid)
  659.     elseif (pose == "Seated") then
  660.         playAnimation("sit", 0.5, Humanoid)
  661.         return
  662.     elseif (pose == "Running") then
  663.         playAnimation("walk", 0.9, Humanoid)
  664.         if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then    
  665.             if not wasStrafe then
  666.                 doForAllRunTracks(function(trackRec, name)
  667.                     trackRec.track:AdjustWeight(smallButNotZero, blendTime)
  668.                     trackRec.lastWeight = smallButNotZero                          
  669.                 end)               
  670.             end
  671.             setRunSpeed()
  672.             wasStrafe = true
  673.         else
  674.             if wasStrafe then
  675.                 doForAllRunTracks(function(trackRec, name)
  676.                     trackRec.track:AdjustWeight(smallButNotZero, blendTime)
  677.                     trackRec.lastWeight = smallButNotZero                          
  678.                 end)
  679.                 setRunSpeed()      
  680.                 wasStrafe = false
  681.             end
  682.         end
  683.     elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  684.         stopAllAnimations()
  685.         amplitude = 0.1
  686.         frequency = 1
  687.         setAngles = true
  688.     end
  689. end
  690.  
  691. -- connect events
  692. Humanoid.Died:connect(onDied)
  693. Humanoid.Running:connect(onRunning)
  694. Humanoid.Jumping:connect(onJumping)
  695. Humanoid.Climbing:connect(onClimbing)
  696. Humanoid.GettingUp:connect(onGettingUp)
  697. Humanoid.FreeFalling:connect(onFreeFall)
  698. Humanoid.FallingDown:connect(onFallingDown)
  699. Humanoid.Seated:connect(onSeated)
  700. Humanoid.PlatformStanding:connect(onPlatformStanding)
  701. Humanoid.Swimming:connect(onSwimming)
  702.  
  703. -- initialize to idle
  704. playAnimation("idle", 0.1, Humanoid)
  705. pose = "Standing"
  706.  
  707. -- loop to handle timed state transitions and tool animations
  708. while Character.Parent ~= nil do
  709.     local _, currentGameTime = wait(0.1)
  710.     stepAnimate(currentGameTime)
  711. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement