Hello i am working on a custom movement system but i have ran into a issue,
when i try to abruptly change the players move direction it will freeze
video of issue:
here is the place file:
customMovment.rbxl (90.5 KB)
code:
function Controller:Movement()--Movement funciton
uis.InputBegan:Connect(function(input) --moveforw
if input.KeyCode == Enum.KeyCode[self.movements.forw] then
self.moveZ = -1
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.forw] then
self.moveZ = 0
end
end)
uis.InputBegan:Connect(function(input) --moveleft
if input.KeyCode == Enum.KeyCode[self.movements.left] then
self.moveX = -1
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.left] then
self.moveX = 0
end
end)
uis.InputBegan:Connect(function(input) --moveback
if input.KeyCode == Enum.KeyCode[self.movements.back] then
self.moveZ = 1
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.back] then
self.moveZ = 0
end
end)
uis.InputBegan:Connect(function(input) --moveright
if input.KeyCode == Enum.KeyCode[self.movements.righ] then
self.moveX = 1
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.righ] then
self.moveX = 0
end
end)
end
function Controller:Update()
local X = self.moveX * 20
local Z = self.moveZ * 20
self.playerCapusel.PrimaryPart.AssemblyLinearVelocity = Vector3.new(X, 0, Z)
--print("X axis: " .. X, "Z axis: " .. Z)
end
Update: i switched to vectorForces which kinda solved the problem
Update/Bump:
i changed the movement code but the problem still occurs
function Controller:Accelerate(accelDir: Vector3, prevVelocity: Vector3, accelerate: number, max_velocity: number, dt)
local projVel = prevVelocity:Dot(accelDir) -- Vector projection of Current velocity onto accelDir.
local accelVel = accelerate * dt -- Accelerated velocity in direction of movment
if (projVel + accelVel > max_velocity) then
accelVel = max_velocity - projVel
end
return prevVelocity + accelDir * accelVel
end
function Controller:MoveGround(accelDir: Vector3, prerVel: Vector3, dt)
local speed = prerVel.Magnitude
if (speed ~= 0) then
local drop = speed * self.playerStats.friction * dt
prerVel = prerVel * math.max(speed-drop, 0)/speed
end
return self:Accelerate(accelDir, prerVel, self.playerStats.accelerate, self.playerStats.max_velocity_ground, dt)
end
function Controller:MoveAir(accelDir: Vector3, preVel: Vector3, dt)
return self:Accelerate(accelDir, preVel, self.playerStats.accelerate, self.playerStats.max_velocity_air, dt)
end
function Controller:Update(dt)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.forw] then
self.accelDir = Vector3.new(0, 0, -1)
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.forw] then
self.accelDir = Vector3.zero
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.back] then
self.accelDir = Vector3.new(0, 0, 1)
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.back] then
self.accelDir = Vector3.zero
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.left] then
self.accelDir = Vector3.new(-1, 0, 0)
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.left] then
self.accelDir = Vector3.zero
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.righ] then
self.accelDir = Vector3.new(1, 0, 0)
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode[self.movements.righ] then
self.accelDir = Vector3.zero
end
end)
self.playerVelocity = self:MoveGround(self.accelDir, self.playerVelocity, dt)
self.playerCapusel.PrimaryPart.moveForce.Force = self.playerVelocity * 50
end
i assume at this point the problem is occurring because how i set-up userinput, if anyone knows please reply kinda want to get this done before spring break ends
Imo what I do for movement instead of checking inputbegan and inputended, I just check on loop with the function uis:IsKeyDown(). It returns whether or not said key is depressed or not. My theory rn is that on or more inputs don’t get recognized and set the velocity to Zero without you intending it to do so.
local moveDir = Vector3.zero
if uis:IsKeyDown(Enum.KeyCode.W) then
moveDir += Vector3.new(0,0,1)
end
if uis:IsKeyDown(Enum.KeyCode.A) then
moveDir += Vector3.new(1,0,0)
end
if uis:IsKeyDown(Enum.KeyCode.S) then
moveDir += Vector3.new(0,0,-1)
end
if uis:IsKeyDown(Enum.KeyCode.D) then
moveDir += Vector3.new(1,0,0)
end
1 Like
Finally! I got a response. I will try this when I get back on my PC. Thanks dude! 
Np, let me know how this works out for you!
It works thanks bro, have a good day or night