Advertisement
AlewAlow

move

Sep 14th, 2023 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local UserInputService = game:GetService("UserInputService")
  4.  
  5. local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
  6. local Components = require(ReplicatedStorage.Shared.Components)
  7.  
  8. local PLAYER_SPEED = 500
  9. local DASH_FORCE = 500
  10.  
  11. local function boolToInt(bool)
  12.     return if bool then 1 else 0
  13. end
  14.  
  15. local function PlayersMove(world)
  16.     for id, velocity in world:query(Components.Velocity, Components.LocalPlayer) do
  17.         local moveDirection = Vector2.new(
  18.             boolToInt(UserInputService:IsKeyDown(Enum.KeyCode.D)) - boolToInt(UserInputService:IsKeyDown(Enum.KeyCode.A)),
  19.             boolToInt(UserInputService:IsKeyDown(Enum.KeyCode.S)) - boolToInt(UserInputService:IsKeyDown(Enum.KeyCode.W))
  20.         )
  21.        
  22.         local unit = if moveDirection ~= Vector2.new(0, 0)
  23.             then moveDirection.Unit
  24.             else moveDirection
  25.        
  26.         world:insert(id, velocity:patch({
  27.             Velocity = velocity.Velocity + unit * PLAYER_SPEED * Matter.useDeltaTime()
  28.         }))
  29.     end
  30.    
  31.     for _, input, isTyping in Matter.useEvent(UserInputService, "InputBegan") do
  32.         if isTyping or input.KeyCode ~= Enum.KeyCode.E then
  33.             continue
  34.         end
  35.        
  36.         for id, transform, velocity in world:query(Components.Transform, Components.Velocity, Components.LocalPlayer) do
  37.             local angle = math.rad(transform.Rotation)
  38.             local value = Vector2.new(math.cos(angle), math.sin(angle)) * DASH_FORCE
  39.  
  40.             world:insert(id, velocity:patch({
  41.                 Velocity = value,
  42.             }))
  43.         end
  44.     end
  45. end
  46.  
  47. return PlayersMove
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement