What’s the best and yet simple way to implement inertia?
Change the player’s friction or making an movement delay system
Cause I tried to make the player slippery and this script doesn’t seem to work
local function setfriction(character)
for _, part in pairs(character:GetChildren()) do
if part:IsA("BasePart") then
if not part.CustomPhysicalProperties then
part.CustomPhysicalProperties = PhysicalProperties.new(part.Material)
end
local orig = part.CustomPhysicalProperties
local density = 1
local friction = GameBaseStats.BaseFriction
local elasticity = 0.5
local frictionWeight = 1
local elasticityWeight = 1
part.CustomPhysicalProperties = PhysicalProperties.new(density, friction, elasticity, frictionWeight, elasticityWeight)
end
end
end
local desiredMovement = Vector2.new(CVMF.KeyDown("A") and -1 or CVMF.KeyDown("D") and 1 or 0,CVMF.KeyDown("W") and -1 or CVMF.KeyDown("S") and 1 or 0)
if desiredMovement.Magnitude > 0 then
desiredVelocity = 12
else
desiredVelocity = 0
end
if desiredVelocity > 0 then
velocity = Lerp(velocity,desiredVelocity,deltaTime*8)
else
velocity = math.max(velocity-(deltaTime*math.sqrt(velocity)*20),0)
end
local camereRelativeMovementDirection = CFrame.Angles(0,-cameraY,0):VectorToObjectSpace(Vector3.new(movementDirection.X,0,movementDirection.Y))
local endPosition = RootPart.Position + camereRelativeMovementDirection * 4
Humanoid:MoveTo(endPosition)
Humanoid.WalkSpeed = velocity
if desiredMovement.Magnitude > 0 then
movementDirection = desiredMovement
end
Make sure that all the code is in a RenderStepped loop. This way it updates every frame.
As you can see in the code, I first see the direction the player is moving in, after that I determine the velocity of the player and after that set the walkspeed to that velocity. I make another variable and if the desired movement direction is greater than zero then I set the movement direction to the desired movement direction. This way the character will sort of slip and keep moving even after the player stops holding WASD.
I did a bit of tweaking but the character seems to be running into 1 direction after i let go of WASD?
local deltaTime = 0
local velocity = 0
local desiredVelocity = GameBaseStats.BaseWalkspeed
local movementDirection = Vector2.new(0, 0)
runService.RenderStepped:Connect(function(step)
local function Lerp(a, b, t)
return a + (b - a) * t
end
deltaTime = step
local desiredMovement = Vector2.new(
isKeyDown("A") and -1 or isKeyDown("D") and 1 or 0,
isKeyDown("W") and -1 or isKeyDown("S") and 1 or 0
)
if desiredMovement.Magnitude > 0 then
desiredVelocity = GameBaseStats.BaseWalkspeed
else
desiredVelocity = 0
end
if desiredVelocity > 0 then
velocity = Lerp(velocity,desiredVelocity,deltaTime*8)
else
velocity = math.max(velocity - (deltaTime * math.sqrt(velocity) * 20), 0)
end
local camera = workspace.CurrentCamera
local cameraDirection = camera.CFrame.LookVector
local cameraRelativeDirection = Vector3.new(desiredMovement.X, 0, desiredMovement.Y)
local movementWorldDirection = (CFrame.lookAt(Vector3.zero, cameraDirection):VectorToWorldSpace(cameraRelativeDirection)).Unit
local RootPart = game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
local Humanoid = game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
if RootPart and Humanoid then
local endPosition = RootPart.Position + movementWorldDirection * 4
Humanoid:MoveTo(endPosition)
Humanoid.WalkSpeed = velocity
end
if desiredMovement.Magnitude > 0 then
movementDirection = desiredMovement
end
end)
Cause you said " Make sure that all the code is in a RenderStepped loop" (I don’t have any functions that uses RenderStepped right now so I figured that I’d test it there)
local RootPart = game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
local Humanoid = game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
These 2 lines make me wanna DIE. No offense. Please make them better!
Here, I did it for you.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local RootPart = character:WaitForChild("HumanoidRootPart")
local Humanoid = character:WaitForChild("Humanoid")
Second, the issue is being caused because you’re doing:
local cameraRelativeDirection = Vector3.new(desiredMovement.X, 0, desiredMovement.Y)
And I am doing:
local cameraRelativeDirection = Vector3.new(movementDirection.X, 0, movementDirection.Y)
A small difference but you’re using desiredMovement.