Newtrat

[ROBLOX] 2D Moving Character

Apr 15th, 2016
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1. local player = game.Players.LocalPlayer
  2. local RunService = game:GetService('RunService')
  3. local ContextActionService = game:GetService('ContextActionService')
  4.  
  5. local jumping = false
  6. local leftValue, rightValue = 0, 0
  7.  
  8. local function onLeft(actionName, inputState)
  9.     if inputState == Enum.UserInputState.Begin then
  10.         leftValue = 1
  11.     elseif inputState == Enum.UserInputState.End then
  12.         leftValue = 0
  13.     end
  14. end
  15.  
  16. local function onRight(actionName, inputState)
  17.     if inputState == Enum.UserInputState.Begin then
  18.         rightValue = 1
  19.     elseif inputState == Enum.UserInputState.End then
  20.         rightValue = 0
  21.     end
  22. end
  23.  
  24. local function onJump(actionName, inputState)
  25.     if inputState == Enum.UserInputState.Begin then
  26.         jumping = true
  27.     elseif inputState == Enum.UserInputState.End then
  28.         jumping = false
  29.     end
  30. end
  31.  
  32. local function onUpdate()
  33.     if player.Character and player.Character:FindFirstChild('Humanoid') then
  34.         if jumping then
  35.             player.Character.Humanoid.Jump = true
  36.         end
  37.         local moveDirection = rightValue - leftValue
  38.         player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
  39.     end
  40. end
  41.  
  42. RunService:BindToRenderStep('Control', Enum.RenderPriority.Input.Value, onUpdate)
  43.  
  44. ContextActionService:BindAction('Left', onLeft, true, 'a', Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
  45. ContextActionService:BindAction('Right', onRight, true, 'd', Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
  46. ContextActionService:BindAction('Jump', onJump, true, 'w', Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)
Advertisement
Add Comment
Please, Sign In to add comment