Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 KB | None | 0 0
  1. --[[
  2.     Handles the movement animations
  3.    
  4. --]]
  5.  
  6.  
  7. local fade=.2
  8.  
  9. --communicate with roblox animation script
  10.  
  11. local angletodir={'f','lf','l','lb','b','rb','r','rf','f',}
  12. local speeds={
  13.     f=.075,
  14.     lf=.075,
  15.     rf=.075,
  16.     l=.075,
  17.     r=.075,
  18.     lb=.075,
  19.     rb=.075,
  20.     b=.075,
  21. }
  22. local walk={
  23.     f=16,
  24.     lf=16,
  25.     rf=16,
  26.     l=16,
  27.     r=16,
  28.     lb=16,
  29.     rb=16,
  30.     b=16,
  31. }
  32.  
  33. local md={
  34.     dir=nil,
  35.     changed=_G.signal(),
  36.     wsmult=1,--walk speed multiplier
  37.     _changed=_G.signal(),--internally called by CharacterScripts.Animate
  38. }
  39.  
  40. local maid=_G.maid()
  41.  
  42. _G.bindspawned(function(char)
  43.     _G.died(char):bind(function()
  44.         maid()
  45.         if md.dir then
  46.             md.dir=nil
  47.             md.changed(nil)
  48.         end
  49.     end)
  50.    
  51.     local hum=_G.hum
  52.     local hrp=_G.hrp
  53.    
  54.     local anims={}
  55.    
  56.     for _,v in next,script:GetChildren()do
  57.         anims[v.Name]=hum:LoadAnimation(v)
  58.         anims[v.Name].Priority=Enum.AnimationPriority.Core
  59.     end
  60.    
  61.     local track
  62.    
  63.     local function getdir()
  64.         local dir=(((hrp.CFrame-hrp.Position):inverse()*hrp.Velocity)*Vector3.new(1,0,1)).Unit
  65.         local x,z=dir.X,dir.Z
  66.         local a=math.atan2(-x,-z)%(math.pi*2)
  67.         for i=1,#angletodir do
  68.             if a<=math.rad(22.5)then
  69.                 return angletodir[i]
  70.             end
  71.             a=a-math.rad(22.5)*2
  72.         end
  73.     end
  74.    
  75.     local function getanim()
  76.         local dir=getdir()
  77.         if md.dir~=dir then
  78.             md.dir=dir
  79.             md.changed(dir)
  80.         end
  81.         hum.WalkSpeed=walk[dir]*md.wsmult
  82.         return anims[dir],speeds[dir]*(hrp.Velocity*Vector3.new(1,0,1)).magnitude/md.heightscale()
  83.     end
  84.    
  85.     maid(_G.beat:Connect(function(dt)
  86.         if not md.dir then return end
  87.         local new,speed=getanim()
  88.        
  89.         if new==track then
  90.             track:AdjustSpeed(speed)
  91.         else
  92.             local pos=track.TimePosition
  93.             track:Stop(fade/2)
  94.             track=new
  95.             track:Play(fade,1,speed)
  96.             track.TimePosition=pos
  97.         end
  98.     end))
  99.    
  100.     maid(md._changed:bind(function(status)--the CharacterScripts.Animate calls this to tell whether or not character is moving
  101.         if status==(md.dir~=nil)then return end
  102.         if status then
  103.             local speed
  104.             track,speed=getanim()
  105.             track:Play(fade,1,speed)
  106.         else
  107.             md.dir=nil
  108.             md.changed(nil)
  109.             track:Stop(fade*2)
  110.             track=nil
  111.         end
  112.     end))
  113. end)
  114.  
  115. return md
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement