Advertisement
Cakey3101

2D Platformer Game - EP 1 - Movement Script

Oct 5th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | Source Code | 0 0
  1. local Players = game:GetService("Players")
  2. local RunService = game:GetService("RunService")
  3. local ContextActionService = game:GetService("ContextActionService")
  4.  
  5. local Player = Players.LocalPlayer
  6. local Jumping = false
  7. local LeftValue, RightValue = 0, 0
  8.  
  9. local function Left(Action, Input)
  10.     if Input == Enum.UserInputState.Begin then 
  11.         LeftValue = 1
  12.     elseif Input == Enum.UserInputState.End then
  13.         LeftValue = 0
  14.     end
  15. end
  16.  
  17. local function Right(Action, Input)
  18.     if Input == Enum.UserInputState.Begin then
  19.         RightValue = 1
  20.     elseif Input == Enum.UserInputState.End then
  21.         RightValue = 0
  22.     end
  23. end
  24.  
  25. local function Jump(Action, Input)
  26.     if Input == Enum.UserInputState.Begin then
  27.         Jumping = true
  28.     elseif Input == Enum.UserInputState.End then
  29.         Jumping = false
  30.     end
  31. end
  32.  
  33. local function IfMoving()
  34.     if Player.Character and Player.Character:FindFirstChild("Humanoid") then
  35.         if Jumping then
  36.             Player.Character.Humanoid.Jump = true
  37.         end
  38.         local MoveDirection = RightValue - LeftValue
  39.         Player.Character.Humanoid:Move(Vector3.new(MoveDirection, 0, 0), false)
  40.     end
  41. end
  42.  
  43. RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, IfMoving)
  44.  
  45. ContextActionService:BindAction("Left", Left, true, Enum.KeyCode.A, Enum.KeyCode.Left, Enum.KeyCode.DPadLeft, Enum.KeyCode.ButtonR1)
  46. ContextActionService:BindAction("Right", Right, true, Enum.KeyCode.D, Enum.KeyCode.Right, Enum.KeyCode.DPadRight, Enum.KeyCode.ButtonR2)
  47. ContextActionService:BindAction("Jump", Jump, true, Enum.KeyCode.W, Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement