Hi, i made a custom movement and i’m wondering if i can get tips over the script so
that next time i can improve upon it.
here’s module
local Vector3 = {}
Vector3.__index = Vector3
---------- DEFAULTS ----------
local default = {}
default.speed = 5
default.acceleration = 5000
default.maxSpeed = 20
-- vector3
function Vector3.New(x, y ,z)
local self = setmetatable({}, Vector3)
self.x = x or 0
self.y = y or 0
self.z = z or 0
return self
end
function Vector3.moveFoward(self, userInputState)
if (userInputState == Enum.UserInputState.Begin) then
self.x = default.speed * default.acceleration
else
if (userInputState == Enum.UserInputState.End) then
self.x = 0
end
end
end
function Vector3.moveBackwards(self, userInputState)
if (userInputState == Enum.UserInputState.Begin) then
self.x = -default.speed * default.acceleration
else
if (userInputState == Enum.UserInputState.End) then
self.x = 0
end
end
end
function Vector3.moveLeft(self, userInputState)
if (userInputState == Enum.UserInputState.Begin) then
self.z = -default.speed * default.acceleration
else
if (userInputState == Enum.UserInputState.End) then
self.z = 0
end
end
end
function Vector3.moveRight(self, userInputState)
if (userInputState == Enum.UserInputState.Begin) then
self.z = default.speed * default.acceleration
else
if (userInputState == Enum.UserInputState.End) then
self.z = 0
end
end
end
return Vector3
here’s the local script
local contextActionService = game:GetService("ContextActionService")
local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local vector3 = require(replicatedStorage:WaitForChild("Vector3"))
local rootPart = script.Parent:WaitForChild("RootPart")
local camera = game.Workspace:WaitForChild("Camera")
-- attachment
local attachment = Instance.new("Attachment")
attachment.Parent = rootPart
-- force
local force = Instance.new("VectorForce")
force.Attachment0 = attachment
force.Parent = rootPart
local self = vector3.New()
local function onFoward(actionName, userInputState, input)
vector3.moveFoward(self, userInputState)
end
local function onBackwards(actionName, userInputState, input)
vector3.moveBackwards(self, userInputState)
end
local function onLeft(actionName, userInputState, input)
vector3.moveLeft(self, userInputState)
end
local function onRight(actionName, userInputState, input)
vector3.moveRight(self, userInputState)
end
contextActionService:BindAction("forward", onFoward, false, Enum.KeyCode.W)
contextActionService:BindAction("backwards", onBackwards, false, Enum.KeyCode.S)
contextActionService:BindAction("left", onLeft, false, Enum.KeyCode.A)
contextActionService:BindAction("right", onRight, false, Enum.KeyCode.D)
runService.RenderStepped:Connect(function()
local vector3 = Vector3.new(self.x, 0, self.z)
force.Force = vector3
end)