Advertisement
Zhang_Beihei

Untitled

Jun 26th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.65 KB | None | 0 0
  1.  
  2. function Service:LoadIKAnimator(Options)
  3.     if Service.CharacterBeingHandled then
  4.         repeat wait() until (not Service.CharacterBeingHandled)
  5.     end
  6.    
  7.     local PreventLeftAnimator = Options.PreventLeftAnimator
  8.    
  9.     local IK = {}
  10.    
  11.     local Character  = Service.Character
  12.    
  13.     local Humanoid = Character:WaitForChild("Humanoid")
  14.     local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
  15.     local LowerTorso = Character:WaitForChild("LowerTorso")
  16.     local UpperTorso = Character:WaitForChild("UpperTorso")
  17.    
  18.     local LeftUpperArm = Character:WaitForChild("LeftUpperArm")
  19.     local LeftLowerArm = Character:WaitForChild("LeftLowerArm")
  20.     local LeftHand = Character:WaitForChild("LeftHand")
  21.    
  22.     local RightUpperArm = Character:WaitForChild("RightUpperArm")
  23.     local RightLowerArm = Character:WaitForChild("RightLowerArm")
  24.     local RightHand = Character:WaitForChild("RightHand")
  25.    
  26.     local LeftShoulder = LeftUpperArm:WaitForChild("LeftShoulder")
  27.     local LeftElbow = LeftLowerArm:WaitForChild("LeftElbow")
  28.     local LeftWrist = LeftHand:WaitForChild("LeftWrist")
  29.     local LeftGripAttachment = LeftHand:WaitForChild("LeftGripAttachment")
  30.    
  31.     local RightShoulder = RightUpperArm:WaitForChild("RightShoulder")
  32.     local RightElbow = RightLowerArm:WaitForChild("RightElbow")
  33.     local RightWrist = RightHand:WaitForChild("RightWrist")
  34.     local RightGripAttachment = RightHand:WaitForChild("RightGripAttachment")
  35.    
  36.     local HEIGHT_UT_HALF = UpperTorso.Size.Y / 2
  37.     local HEIGHT_UT_QUAD = HEIGHT_UT_HALF / 2
  38.    
  39.     local CF_UT_HALF = CF(0, HEIGHT_UT_HALF, 0)
  40.     local CF_UT_HALF_N = CF(0, -HEIGHT_UT_HALF, 0)
  41.    
  42.     local LSC0 = CF(-UpperTorso.Size.X / 2 - LeftUpperArm.Size.X / 2, UpperTorso.Size.Y / 4,  0)
  43.     local LSC1 = CF(0, LeftUpperArm.Size.Y / 2, 0)
  44.    
  45.     local LEC0 = CF(0, -LeftUpperArm.Size.Y / 2, 0)
  46.     local LEC1 = CF(0,  LeftLowerArm.Size.Y / 2, 0)
  47.    
  48.     local RSC0 = CF(UpperTorso.Size.X / 2 + RightUpperArm.Size.X / 2, UpperTorso.Size.Y / 4, 0)
  49.     local RSC1 = CF(0, RightUpperArm.Size.Y / 2, 0)
  50.    
  51.     local REC0 = CF(0, -RightUpperArm.Size.Y / 2, 0)
  52.     local REC1 = CF(0,  RightLowerArm.Size.Y / 2, 0)
  53.  
  54.     local LL1 =  LeftUpperArm.Size.Y
  55.     local LL2 =  LEC1.Y
  56.          - Service.Joints.LeftWrist.C0.Y
  57.          + Service.Joints.LeftWrist.C1.Y
  58.          - LeftGripAttachment.Position.Y
  59.    
  60.     local RL1 =  RightUpperArm.Size.Y
  61.     local RL2 =  REC1.Y
  62.          - Service.Joints.RightWrist.C0.Y
  63.          + Service.Joints.RightWrist.C1.Y
  64.          - RightGripAttachment.Position.Y
  65.    
  66.     local SolverLeft  = IKSolver.new({
  67.         SizeLimb0 = LL1,
  68.         SizeLimb1 = LL2
  69.     })
  70.    
  71.     local SolverRight = IKSolver.new({
  72.         SizeLimb0 = RL1,
  73.         SizeLimb1 = RL2
  74.     })
  75.    
  76.     local SolverLeftPure  = IKSolver.new({
  77.         SizeLimb0 = LL1,
  78.         SizeLimb1 = LL2 + LeftGripAttachment.Position.Y - Service.Joints.RightWrist.C1.Y
  79.     })
  80.    
  81.     local SolverRightPure = IKSolver.new({
  82.         SizeLimb0 = RL1,
  83.         SizeLimb1 = RL2 + LeftGripAttachment.Position.Y - Service.Joints.RightWrist.C1.Y
  84.     })
  85.    
  86.     local IKSolveLeft = SolverLeft.Solve
  87.     local IKSolveRight = SolverRight.Solve
  88.    
  89.     local IKSolveLeftPure = SolverLeftPure.Solve
  90.     local IKSolveRightPure = SolverRightPure.Solve
  91.    
  92.     local StopServerUpdateData = {}
  93.    
  94.     StopServerUpdateData[#StopServerUpdateData + 1] = {Service.Joints.RightShoulder.Joint, Service.Joints.RightShoulder.C0, Service.Joints.RightShoulder.C1}
  95.     StopServerUpdateData[#StopServerUpdateData + 1] = {Service.Joints.RightElbow.Joint, Service.Joints.RightElbow.C0, Service.Joints.RightElbow.C1}
  96.     StopServerUpdateData[#StopServerUpdateData + 1] = {Service.Joints.RightWrist.Joint, Service.Joints.RightWrist.C0}
  97.    
  98.     StopServerUpdateData[#StopServerUpdateData + 1] = {Service.Joints. LeftShoulder.Joint, Service.Joints. LeftShoulder.C0, Service.Joints.LeftShoulder.C1}
  99.     StopServerUpdateData[#StopServerUpdateData + 1] = {Service.Joints. LeftElbow.Joint, Service.Joints. LeftElbow.C0, Service.Joints.LeftElbow.C1}
  100.     StopServerUpdateData[#StopServerUpdateData + 1] = {Service.Joints. LeftWrist.Joint, Service.Joints. LeftWrist.C0}
  101.    
  102.     if Options.LookAroundConstraints then
  103.         StopServerUpdateData[#StopServerUpdateData + 1] = {Service.Joints.Waist.Joint, Service.Joints.Waist.C0}
  104.         StopServerUpdateData[#StopServerUpdateData + 1] = {Service.Joints.Neck .Joint, Service.Joints.Neck .C0}
  105.     end
  106.    
  107.     IK.OverrideLook = nil
  108.    
  109.     IK.UpdateQueue = {}
  110.     IK.UpdateFuncs = {}
  111.     IK.Connections = {}
  112.    
  113.     IK.Events = {}
  114.    
  115.     IK.State = {}
  116.     IK.State.Playing = false
  117.     IK.State.Paused = false
  118.     IK.State.LastStartTime = 0
  119.     IK.State.LastStopTime  = 0
  120.     IK.State.AlignCharacter = false
  121.    
  122.     local LastServerUpdate = 0
  123.     local LastServerUpdateData = {}
  124.     local ServerRefreshWait = 0.1
  125.    
  126.     IK.GetGoalLeft  = Options.GetGoalLeft
  127.     IK.GetGoalRight = Options.GetGoalRight
  128.    
  129.     local UpdateFuncs = IK.UpdateFuncs
  130.     local UpdateQueue = IK.UpdateQueue
  131.     local Connections = IK.Connections
  132.     local Events = IK.Events
  133.     local State  = IK.State
  134.    
  135.     local TRANS_FACTOR = Service.Constants.TRANS_FACTOR
  136.     local TRANS_FIXED_STEP = Service.Constants.TRANS_FIXED_STEP
  137.    
  138.     function IK:SetGoalLeft(F)
  139.         IK.GetGoalLeft = F
  140.     end
  141.    
  142.     function IK:SetGoalRight(F)
  143.         IK.GetGoalRight = F
  144.     end
  145.    
  146.     function IK:CanAlignCharacter()
  147.         return Humanoid:GetState() ~= Enum.HumanoidStateType.Seated
  148.     end
  149.    
  150.     function IK:SetCharacterAlignEnabled(Enabled)
  151.         State.AlignCharacter = Enabled
  152.     end
  153.    
  154.     function IK:AddToUpdateQueue(F)
  155.         UpdateQueue[#UpdateQueue + 1] = F
  156.     end
  157.    
  158.     function IK:AddToUpdateQueuePerma(F)
  159.         UpdateFuncs[#UpdateFuncs + 1] = F
  160.     end
  161.    
  162.     local function Flush()
  163.         RemoteService.Remotes.Event:FireServer(
  164.             "ServerMotor6DReceive", LastServerUpdateData, 0.15
  165.         )
  166.     end
  167.    
  168.     function IK:SetupAlignCharacter()
  169.         local CanAlignCharacter = IK.CanAlignCharacter
  170.        
  171.         local RootJoint = Service.Joints.Root
  172.         local C0 = RootJoint.C0
  173.         local Motor6D = RootJoint.Joint
  174.        
  175.         local _, AnglePosInit = C0:toEulerAnglesXYZ()
  176.         local _, AnglePos     = Motor6D.C0:toEulerAnglesXYZ()
  177.        
  178.         local MotorC0 = Angles(0, AnglePos, 0)
  179.         local ServerData = {Motor6D, MotorC0}
  180.        
  181.         IK:AddToUpdateQueue(function(Now, Delta, Alpha, Hit, CF_HRP, CF_UT)
  182.             debug.profilebegin("IKAlignCharacter")
  183.             local Azimuth = GetPolar3(Polar3, CF_HRP:inverse() * Hit)
  184.            
  185.             local AnglePosDesired = (
  186.                 State.AlignCharacter and CanAlignCharacter()
  187.             ) and -(Azimuth + PI_HALF) or AnglePosInit
  188.            
  189.             AnglePos = AnglePos
  190.                  + ((AnglePosDesired - AnglePos + PI) % PI_2 - PI)
  191.                  * clamp(1 - pow(1 - 0.2, 60 * Delta), 0, 1)
  192.            
  193.             MotorC0 = C0 * Angles(0,
  194.                 AnglePosInit + ((AnglePos - AnglePosInit + PI) % PI_2 - PI) * Alpha, 0)
  195.             Motor6D.C0 = MotorC0
  196.             ServerData[2] = MotorC0
  197.            
  198.             debug.profileend()
  199.             return ServerData
  200.         end)
  201.     end
  202.    
  203.     function IK:SetupLookAround(Options)
  204.         local RootJoint = Service.Joints.Root
  205.         local C0 = RootJoint.C0
  206.         local Motor6D = RootJoint.Joint
  207.        
  208.         local WaistData = Service.Joints.Waist
  209.         local Waist = WaistData.Joint
  210.         local WaistC0 = WaistData.C0
  211.         local WaistC1 = WaistData.C1
  212.        
  213.         local NeckData = Service.Joints.Neck
  214.         local Neck = NeckData.Joint
  215.         local NeckC0 = NeckData.C0
  216.         local NeckC1 = NeckData.C1
  217.        
  218.         local RootData = Service.Joints.Root
  219.         local RootC0 = RootData.C0
  220.         local RootC1 = RootData.C1
  221.        
  222.         local HeightAim = RootC0.Y - RootC1.Y + WaistC0.Y - WaistC1.Y + UpperTorso.Size.Y / 4
  223.         local HeightAimCF = CF(0, HeightAim, 0)
  224.         local HeightAimLT = WaistC0.Y - WaistC1.Y + UpperTorso.Size.Y / 4
  225.         local HeightAimLTCF = CF(0, HeightAimLT, 0)
  226.        
  227.         local AngleX = Options.LimitTorsoAzimuth
  228.         local AngleY = Options.LimitTorsoElevation
  229.        
  230.         local CurrX, CurrY = 0, 0
  231.         local HeadA, HeadE = 0, 0
  232.        
  233.         local ServerDataWaist = {Waist, WaistC0}
  234.         local ServerDataNeck  = {Neck , NeckC0 }
  235.        
  236.         local VECTOR3_MULT_ZN = V3(1, 1, -1)
  237.        
  238.         IK:AddToUpdateQueue(function(Now, Delta, Alpha, Hit, CF_HRP, CF_UT)
  239.             debug.profilebegin("IKLookAround")
  240.            
  241.             local LocalSpace = LookAtUV(Vector, CF_HRP.p, CF_HRP.p + CF_HRP.upVector, LowerTorso.CFrame.lookVector)
  242.             local LocalSpaceHit = LocalSpace:pointToObjectSpace(Hit)
  243.            
  244.             if LocalSpaceHit.Z > 0 then
  245.                 Hit = LocalSpace:pointToWorldSpace(LocalSpaceHit * VECTOR3_MULT_ZN)
  246.             end
  247.            
  248.             local Azimuth, Elevation = GetPolar3(
  249.                 Polar3, (LowerTorso.CFrame * HeightAimLTCF):pointToObjectSpace(Hit)
  250.             )
  251.            
  252.             local X, Y  = (Azimuth * (Azimuth > 0 and -1 or 1) + PI_HALF) / PI_HALF, Elevation / PI_HALF
  253.             local Scale = clamp(1 - pow(1 - 0.1, 60 * Delta), 0, 1)
  254.            
  255.             CurrX = CurrX + (X - CurrX) * Scale
  256.             CurrY = CurrY + (Y - CurrY) * Scale
  257.            
  258.             CurrX = CurrX * Alpha
  259.             CurrY = CurrY * Alpha
  260.            
  261.             --[[
  262.             local WC0 = WaistC0 * FromAxisAngle((LowerTorso.CFrame * WaistC0):vectorToObjectSpace(
  263.                 CF_HRP.upVector
  264.             ), -CurrX * AngleX * Alpha) * Angles(CurrY * AngleY * Alpha, 0, 0)
  265.             ]]
  266.            
  267.             local WC0 = WaistC0 * Angles(CurrY * AngleY * Alpha, -CurrX * AngleX * Alpha, 0)
  268.             Waist.C0 = WC0
  269.             ServerDataWaist[2] = WC0
  270.            
  271.             local A, E = GetPolar3(Polar3, (
  272.                 CF_UT * CF_UT_HALF
  273.             ):inverse() * Hit)
  274.            
  275.             A = A > 0 and -A or A
  276.            
  277.             HeadA = HeadA + (A - HeadA) * Scale
  278.             HeadE = HeadE + (E - HeadE) * Scale
  279.            
  280.             HeadA = HeadA + ((PI_HALF - HeadA) % PI_2 - PI) * (1 - Alpha)
  281.             HeadE = HeadE * Alpha
  282.            
  283.             local NC0 = NeckC0 * LookAt(Vector,
  284.                 ZERO_VECTOR3, GetVector3(Polar3, HeadA, HeadE, 1), VECTOR3_UP
  285.             )
  286.            
  287.             Neck.C0 = NC0
  288.             ServerDataNeck[2] = NC0
  289.             debug.profileend()
  290.             return ServerDataWaist, ServerDataNeck
  291.         end)
  292.     end
  293.    
  294.     local C0LeftShoulder = Service.Joints.LeftShoulder.C0
  295.     local C1LeftShoulder = Service.Joints.LeftShoulder.C1
  296.     local C0LeftElbow = Service.Joints.LeftElbow.C0
  297.     local C1LeftElbow = Service.Joints.LeftElbow.C1
  298.     local C0LeftWrist = Service.Joints.LeftWrist.C0
  299.     local C1LeftWrist = Service.Joints.LeftWrist.C1
  300.     local CFLeftGripInv = LeftGripAttachment.CFrame:inverse()
  301.    
  302.     local C0RightShoulder = Service.Joints.RightShoulder.C0
  303.     local C1RightShoulder = Service.Joints.RightShoulder.C1
  304.     local C0RightElbow = Service.Joints.RightElbow.C0
  305.     local C1RightElbow = Service.Joints.RightElbow.C1
  306.     local C0RightWrist = Service.Joints.RightWrist.C0
  307.     local C1RightWrist = Service.Joints.RightWrist.C1
  308.     local CFRightGripInv = RightGripAttachment.CFrame:inverse()
  309.    
  310.     local ServerDataLeftShoulder = {LeftShoulder, LeftShoulder.C0}
  311.     local ServerDataLeftElbow = {LeftElbow, LeftElbow.C0}
  312.     local ServerDataLeftWrist = {LeftWrist, LeftWrist.C0}
  313.    
  314.     local ServerDataRightShoulder = {RightShoulder, RightShoulder.C0}
  315.     local ServerDataRightElbow = {RightElbow, RightElbow.C0}
  316.     local ServerDataRightWrist = {RightWrist, RightWrist.C0}
  317.    
  318.     local OffsetLeft = CF(-UpperTorso.Size.X / 2 - LeftUpperArm.Size.X / 2, UpperTorso.Size.Y / 4, 0)
  319.     local OffsetLeftLowerArm = CF(0, -LeftLowerArm.Size.Y, 0)
  320.    
  321.     local OffsetRight = CF(UpperTorso.Size.X / 2 + RightUpperArm.Size.X / 2, UpperTorso.Size.Y / 4, 0)
  322.     local OffsetRightLowerArm = CF(0, -RightLowerArm.Size.Y, 0)
  323.    
  324.     local function UpdateLeft(Now, Delta, Alpha, Hit, CF_HRP, CF_UT)
  325.         debug.profilebegin("IKUpdateLeft")
  326.         local GetGoalLeft = IK.GetGoalLeft
  327.         local Playing = State.Playing
  328.         local LSC0, LEC0, LWC0
  329.        
  330.         if not Playing and Alpha ~= 1 then
  331.             LSC0 = C0LeftShoulder:lerp(LeftShoulder.C0, Alpha)
  332.             LEC0 = C0LeftElbow:lerp(LeftElbow.C0, Alpha)
  333.             LWC0 = C0LeftWrist:lerp(LeftWrist.C0, Alpha)
  334.         elseif GetGoalLeft then
  335.             local CFShoulder = CF_UT * LeftShoulder.C0--* OffsetLeft
  336.             local Goal, GoalElbow, GripCFrame = GetGoalLeft(Now, Delta, Alpha, Hit, CF_HRP, CF_UT)
  337.            
  338.             local JointCFrameShoulder, JointCFrameElbow
  339.            
  340.             if GripCFrame then
  341.                 local GoalCF = GripCFrame * CFLeftGripInv * C1LeftWrist
  342.                
  343.                 Goal = GoalCF.p
  344.                
  345.                 JointCFrameShoulder, JointCFrameElbow = IKSolveLeftPure(
  346.                     SolverLeft, Goal, GoalElbow, CFShoulder,
  347.                     CFShoulder * LeftShoulder.C1:inverse() * LeftElbow.C0
  348.                 )
  349.                
  350.                 JointCFrameElbow = JointCFrameElbow
  351.                      + (Goal - (JointCFrameElbow * OffsetLeftLowerArm).p) / 2
  352.                
  353.                 LWC0 = (
  354.                     JointCFrameElbow * LeftElbow.C1:inverse()
  355.                 ):toObjectSpace(GoalCF)
  356.             else
  357.                 JointCFrameShoulder, JointCFrameElbow = IKSolveLeft(
  358.                     SolverLeft, Goal, GoalElbow, CFShoulder,
  359.                     LeftLowerArm.CFrame * LeftElbow.C0
  360.                 )
  361.                
  362.                 LWC0 = C0LeftWrist
  363.             end
  364.            
  365.             LSC0 = CF_UT:toObjectSpace(JointCFrameShoulder * CFrame.Angles(0, -math.pi / 2, 0))
  366.             LEC0 = (
  367.                 JointCFrameShoulder * LeftShoulder.C1:inverse() * CFrame.Angles(0, -math.pi / 2, 0)
  368.             ):toObjectSpace(JointCFrameElbow)
  369.         end
  370.        
  371.         LeftShoulder.C0 = LSC0
  372.         LeftElbow.C0 = LEC0
  373.         LeftWrist.C0 = LWC0
  374.        
  375.         ServerDataLeftShoulder[2] = LSC0
  376.         ServerDataLeftElbow[2] = LEC0
  377.         ServerDataLeftWrist[2] = LWC0
  378.         debug.profileend()
  379.        
  380.         return ServerDataLeftShoulder, ServerDataLeftElbow, ServerDataLeftWrist
  381.     end
  382.    
  383.     local ANGLES_PI = Angles(0, PI, 0)
  384.    
  385.     local function UpdateRight(Now, Delta, Alpha, Hit, CF_HRP, CF_UT)
  386.         debug.profilebegin("IKUpdateRight")
  387.         local GetGoalRight = IK.GetGoalRight
  388.         local Playing = State.Playing
  389.         local RSC0, REC0, RWC0
  390.        
  391.         if not Playing and Alpha ~= 1 then
  392.             RSC0 = C0RightShoulder:lerp(RightShoulder.C0, Alpha)
  393.             REC0 = C0RightElbow:lerp(RightElbow.C0, Alpha)
  394.             RWC0 = C0RightWrist:lerp(RightWrist.C0, Alpha)
  395.         elseif GetGoalRight then
  396.             local CFShoulder = CF_UT * RightShoulder.C0--* OffsetRight
  397.             local Goal, GoalElbow, GripCFrame = GetGoalRight(Now, Delta, Alpha, Hit, CF_HRP, CF_UT)
  398.            
  399.             local JointCFrameShoulder, JointCFrameElbow
  400.            
  401.             if GripCFrame then
  402.                 local GoalCF = GripCFrame * CFRightGripInv * C1RightWrist
  403.                
  404.                 Goal = GoalCF.p
  405.                
  406.                 JointCFrameShoulder, JointCFrameElbow = IKSolveRightPure(
  407.                     SolverRight, Goal, GoalElbow, CFShoulder,
  408.                     CFShoulder * RightShoulder.C1:inverse() * RightElbow.C0
  409.                 )
  410.                
  411.                 JointCFrameElbow = JointCFrameElbow
  412.                      + (Goal - (JointCFrameElbow * OffsetRightLowerArm).p) / 2
  413.                
  414.                 RWC0 = (
  415.                     JointCFrameElbow * RightElbow.C1:inverse()
  416.                 ):toObjectSpace(GoalCF)
  417.             else
  418.                 JointCFrameShoulder, JointCFrameElbow = IKSolveRight(
  419.                     SolverRight, Goal, GoalElbow, CFShoulder,
  420.                     CFShoulder * RightShoulder.C1:inverse() * RightElbow.C0
  421.                 )
  422.                
  423.                 RWC0 = C0RightWrist
  424.             end
  425.            
  426.             RSC0 = CF_UT:toObjectSpace(JointCFrameShoulder) * Angles(0, -math.pi / 2, 0) --* ANGLES_PI
  427.             REC0 = (
  428.                 JointCFrameShoulder * RSC1:inverse() * Angles(0, -math.pi / 2, 0)
  429.             ):toObjectSpace(JointCFrameElbow)
  430.         end
  431.        
  432.         RightShoulder.C0 = RSC0
  433.         RightElbow.C0 = REC0
  434.         RightWrist.C0 = RWC0
  435.        
  436.         ServerDataRightShoulder[2] = RSC0
  437.         ServerDataRightElbow[2] = REC0
  438.         ServerDataRightWrist[2] = RWC0
  439.         debug.profileend()
  440.        
  441.         return ServerDataRightShoulder, ServerDataRightElbow, ServerDataRightWrist
  442.     end
  443.    
  444.     local ServerDataRSC1 = {RightShoulder, nil, RightShoulder.C1}
  445.     local ServerDataREC1 = {RightElbow, nil, RightElbow.C1}
  446.    
  447.     local ServerDataLSC1 = {LeftShoulder, nil, LeftShoulder.C1}
  448.     local ServerDataLEC1 = {LeftElbow, nil, LeftElbow.C1}
  449.    
  450.     function IK:Update(Delta, ForceUpdateQueue)
  451.         debug.profilebegin("IKUpdate")
  452.         local UpdateQueue = ForceUpdateQueue or UpdateQueue
  453.         local Now = tick()
  454.         local Hit = IK.OverrideLook and IK.OverrideLook(Now, Delta) or LocalPlayerMouse.Hit.p
  455.         local Alpha = State.Playing and
  456.                 (Now - State.LastStartTime) / 0.3 or
  457.             1 - (Now - State.LastStopTime ) / 0.3
  458.        
  459.         Alpha = Alpha < 0 and 0 or Alpha > 1 and 1 or Alpha
  460.        
  461.         RightShoulder.C1 = C1RightShoulder:lerp(RSC1, Alpha)
  462.         RightElbow.C1 = C1RightElbow:lerp(REC1, Alpha)     
  463.        
  464.         ServerDataRSC1[3] = RightShoulder.C1
  465.         ServerDataREC1[3] = RightElbow.C1
  466.        
  467.         if not PreventLeftAnimator then
  468.             LeftShoulder.C1 = C1LeftShoulder:lerp(LSC1, Alpha)
  469.             LeftElbow.C1 = C1LeftElbow:lerp(LEC1, Alpha)
  470.            
  471.             ServerDataLEC1[3] = LeftElbow.C1
  472.             ServerDataLSC1[3] = LeftShoulder.C1
  473.         end
  474.        
  475.         local ServerUpdate = Now - LastServerUpdate > ServerRefreshWait
  476.        
  477.         if ServerUpdate then
  478.             LastServerUpdate = Now
  479.             LastServerUpdateData = {
  480.                 ServerDataRSC1,
  481.                 ServerDataREC1
  482.             }
  483.            
  484.             if not PreventLeftAnimator then
  485.                 LastServerUpdateData[#LastServerUpdateData + 1] = ServerDataLSC1
  486.                 LastServerUpdateData[#LastServerUpdateData + 1] = ServerDataLEC1
  487.             end
  488.         end
  489.        
  490.         for _, QueueItem in pairs(UpdateQueue) do
  491.             local ServerMotorData = {QueueItem(Now, Delta, Alpha, Hit, HumanoidRootPart.CFrame, UpperTorso.CFrame)}
  492.            
  493.             if ServerUpdate then
  494.                 for _, Data in pairs(ServerMotorData) do
  495.                     LastServerUpdateData[#LastServerUpdateData + 1] = Data
  496.                 end
  497.             end
  498.         end
  499.        
  500.         local ServerMotorData = {}
  501.        
  502.         ServerMotorData[1], ServerMotorData[2], ServerMotorData[3] =
  503.             UpdateRight(Now, Delta, Alpha, Hit, HumanoidRootPart.CFrame, UpperTorso.CFrame)
  504.        
  505.         if not PreventLeftAnimator then
  506.             ServerMotorData[4], ServerMotorData[5], ServerMotorData[6] =
  507.                 UpdateLeft(Now, Delta, Alpha, Hit, HumanoidRootPart.CFrame, UpperTorso.CFrame)
  508.         else
  509.             ServerMotorData[4], ServerMotorData[5], ServerMotorData[6] =
  510.                 {LeftShoulder, C0LeftShoulder, C1LeftShoulder},
  511.                 {LeftWrist, C0LeftWrist, C1LeftWrist},
  512.                 {LeftElbow, C0LeftElbow, C1LeftElbow}
  513.         end
  514.        
  515.         if ServerUpdate then
  516.             for _, Data in pairs(ServerMotorData) do
  517.                 LastServerUpdateData[#LastServerUpdateData + 1] = Data
  518.             end
  519.            
  520.             Flush()
  521.         end
  522.        
  523.         debug.profileend()
  524.         return IK
  525.     end
  526.    
  527.     local ServerDataC1 = {
  528.         {RightShoulder, nil, C1RightShoulder},
  529.         {RightElbow, nil, C1RightElbow}
  530.     }
  531.    
  532.     if not PreventLeftAnimator then
  533.         ServerDataC1[#ServerDataC1 + 1] = {LeftShoulder, nil, C1LeftShoulder}
  534.         ServerDataC1[#ServerDataC1 + 1] = {LeftElbow, nil, C1LeftElbow}
  535.     end
  536.    
  537.     function IK:Fade(UpdateQueue, Stopped)
  538.         local LastStopTime = State.LastStopTime
  539.         local Connection
  540.         --local ConnectionStepped
  541.         local Disconnected
  542.         local Result = {}
  543.        
  544.         local function Disconnect()
  545.             if Disconnected then return end
  546.            
  547.             Disconnected = true
  548.             Connection:Disconnect()
  549.         end
  550.        
  551.         Connection = RunService.RenderStepped:Connect(function(Delta)
  552.             if
  553.                 (not Result.Stopped and Service.CurrentResponsiveAnimation ~= IK) or
  554.                 (    Result.Stopped and Service.CurrentResponsiveAnimation)
  555.             then
  556.                 Connection:Disconnect()
  557.                 --ConnectionStepped:Disconnect()
  558.                
  559.                 RightShoulder.C1 = C1RightShoulder
  560.                 RightElbow.C1 = C1RightElbow
  561.                
  562.                 if not PreventLeftAnimator then
  563.                     LeftShoulder.C1 = C1LeftShoulder
  564.                     LeftElbow.C1 = C1LeftElbow
  565.                 end
  566.                
  567.                 RemoteService.Remotes.Event:FireServer(
  568.                     "ServerMotor6DReceive", ServerDataC1, 0.15
  569.                 )
  570.             else
  571.                 IK:Update(Delta, UpdateQueue)
  572.                
  573.                 if tick() - LastStopTime > 0.3 then
  574.                     Disconnect()
  575.                     RemoteService.Remotes.Event:FireServer(
  576.                         "ServerMotor6DReceive", IK.StopServerUpdateData, 0.15)
  577.                 end
  578.             end
  579.         end)
  580.        
  581.         --ConnectionStepped = RunService.Stepped:Connect(IK.NegateTransform)
  582.        
  583.         Result.Stopped = Stopped
  584.         Result.Disconnect = Disconnect
  585.        
  586.         return Result
  587.     end
  588.     --[[
  589.     function IK:NegateTransform()
  590.         local Now = tick()
  591.        
  592.         local Alpha = State.Playing and
  593.             clamp((Now - State.LastStartTime) / 0.3, 0, 1) or
  594.             clamp(1 - (Now - State.LastStopTime ) / 0.3, 0, 1)
  595.            
  596.         if Alpha == 1 then
  597.             if Options.LookAroundConstraints then
  598.                 Service.Joints.Waist.Joint.Transform = ZERO_CFRAME
  599.                 Service.Joints.Neck.Joint.Transform = ZERO_CFRAME
  600.             end
  601.            
  602.             RightShoulder.Transform = ZERO_CFRAME
  603.             RightElbow.Transform = ZERO_CFRAME
  604.             RightWrist.Transform = ZERO_CFRAME
  605.            
  606.             LeftShoulder.Transform = ZERO_CFRAME
  607.             LeftElbow.Transform = ZERO_CFRAME
  608.             LeftWrist.Transform = ZERO_CFRAME
  609.         else
  610.             Alpha = 1 - Alpha
  611.            
  612.             if Options.LookAroundConstraints then
  613.                 Service.Joints.Waist.Joint.Transform = ZERO_CFRAME:lerp(Service.Joints.Waist.Joint.Transform, Alpha)
  614.                 Service.Joints.Neck.Joint.Transform = ZERO_CFRAME:lerp(Service.Joints.Neck.Joint.Transform, Alpha)
  615.             end
  616.            
  617.             RightShoulder.Transform = ZERO_CFRAME:lerp(RightShoulder.Transform, Alpha)
  618.             RightElbow.Transform = ZERO_CFRAME:lerp(RightElbow.Transform, Alpha)
  619.             RightWrist.Transform = ZERO_CFRAME:lerp(RightWrist.Transform, Alpha)
  620.            
  621.             LeftShoulder.Transform = ZERO_CFRAME:lerp(LeftShoulder.Transform, Alpha)
  622.             LeftElbow.Transform = ZERO_CFRAME:lerp(LeftElbow.Transform, Alpha)
  623.             LeftWrist.Transform = ZERO_CFRAME:lerp(LeftWrist.Transform, Alpha)
  624.         end
  625.     end]]
  626.    
  627.     function IK:Play()
  628.         if State.Playing then return IK end
  629.        
  630.         if State.Paused then
  631.             if Service.CurrentResponsiveAnimation ~= IK then
  632.                 Service.CurrentResponsiveAnimation:Stop()
  633.             end
  634.            
  635.             State.Paused:Disconnect()
  636.             State.Paused = false
  637.         else
  638.             if Service.CurrentResponsiveAnimation then
  639.                 Service.CurrentResponsiveAnimation:Stop()
  640.             end
  641.            
  642.             IK:SetupAlignCharacter()
  643.            
  644.             if Options.LookAroundConstraints then
  645.                 IK:SetupLookAround(Options.LookAroundConstraints)
  646.             end
  647.            
  648.             for _, Produce in pairs(IK.UpdateFuncs) do
  649.                 UpdateQueue[#UpdateQueue + 1] = Produce()
  650.             end
  651.         end
  652.        
  653.         local Now = tick()
  654.         local StopTimeDelta = Now - State.LastStopTime
  655.        
  656.         State.Playing = true
  657.         State.LastStartTime = StopTimeDelta < 0.3 and Now - 0.3 + StopTimeDelta or Now
  658.        
  659.         if PreventLeftAnimator then
  660.             Service.Animations.Tracks.TorsoRight:Play(0)
  661.         else
  662.             Service.Animations.Tracks.Freeze:Play(0)
  663.         end
  664.        
  665.         IK.Connections.Update = RunService.RenderStepped:Connect(
  666.             General:ClassWrap(IK, IK.Update))
  667.        
  668.         if PreventLeftAnimator then
  669.             LeftShoulder.C0 = C0LeftShoulder
  670.             LeftShoulder.C1 = C1LeftShoulder
  671.             LeftElbow.C0 = C0LeftElbow
  672.             LeftElbow.C1 = C1LeftElbow
  673.             LeftWrist.C0 = C0LeftWrist
  674.             LeftWrist.C1 = C1LeftWrist
  675.         end
  676.        
  677.         --Connections.NegateTransform = RunService.Stepped:Connect(IK.NegateTransform)
  678.        
  679.         Service.CurrentResponsiveAnimation = IK
  680.        
  681.         return IK
  682.     end
  683.    
  684.     function IK:Stop()
  685.         if not State.Playing and not State.Paused then return IK end
  686.        
  687.         if State.Paused then
  688.             State.Paused.Stopped = true
  689.             State.Paused = false
  690.         else
  691.             local Now = tick()
  692.             local StartTimeDelta = Now - State.LastStartTime
  693.            
  694.             State.Playing = false
  695.             State.LastStopTime = StartTimeDelta < 0.3 and Now - 0.3 + StartTimeDelta or Now
  696.            
  697.             General:ClearConnections(Connections)
  698.            
  699.             if Service.CurrentResponsiveAnimation == IK then
  700.                 Service.CurrentResponsiveAnimation = nil
  701.             end
  702.            
  703.             if PreventLeftAnimator then
  704.                 Service.Animations.Tracks.TorsoRight:Stop(0.3)
  705.             else
  706.                 Service.Animations.Tracks.Freeze:Stop(0.3)
  707.             end
  708.            
  709.             IK:Fade(UpdateQueue, true)
  710.         end
  711.        
  712.         IK.UpdateQueue = {}
  713.         UpdateQueue = {}
  714.        
  715.         return IK
  716.     end
  717.    
  718.     function IK:Pause()
  719.         if not State.Playing then return IK end
  720.        
  721.         General:ClearConnections(Connections)
  722.        
  723.         local Now = tick()
  724.         local StartTimeDelta = Now - State.LastStartTime
  725.        
  726.         State.Playing = false
  727.         State.LastStopTime = StartTimeDelta < 0.3 and Now - 0.3 + StartTimeDelta or Now
  728.         State.Paused = IK:Fade(UpdateQueue)
  729.        
  730.         if PreventLeftAnimator then
  731.             Service.Animations.Tracks.TorsoRight:Stop(0.3)
  732.         else
  733.             Service.Animations.Tracks.Freeze:Stop(0.3)
  734.         end
  735.        
  736.         return IK
  737.     end
  738.    
  739.     return IK
  740. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement