Calculating Things Acceleration?

Im trying to but I do not know if Im doing it right here is my code. I f you could help me Im trying to do dea cceleration as well that would be swell.

Code:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local LocalPlayerCharacter = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local LocalPlayerHumanoid = LocalPlayerCharacter:WaitForChild("Humanoid")

function GetMass(Model)
	local Mass = 0
	for Number, Instance2 in pairs(Model:GetDescendants()) do
		if Instance2.ClassName == "Part" then
			Mass = Mass + Instance2.Mass
		end
	end
	return Mass
end

local ChangeInVelocity = LocalPlayerHumanoid.WalkSpeed
local ChangeInTime = 2.5
local Acceleration = ChangeInVelocity / ChangeInTime
local Mass = GetMass(LocalPlayerCharacter)
local Force = Mass * Acceleration
local Acceleration = Force / Mass

local InputBegan = false

UserInputService.InputBegan:Connect(function(InputBegan)
	if InputBegan.KeyCode == Enum.KeyCode.W  then
		InputBegan = true
		while wait(ChangeInTime) and InputBegan do
			LocalPlayerHumanoid.WalkSpeed = LocalPlayerHumanoid.WalkSpeed + Acceleration 
		end
	end
end)

UserInputService.InputEnded:Connect(function(InputEnded)
	if InputEnded.KeyCode == Enum.KeyCode.W then
		InputBegan = false
	end
end)
local Force = Mass * Acceleration
local Acceleration = Force / Mass

This won’t work because neither variable is inside of their scopes. You want to either plug in force or acceleration yourself

I thought you constantly updated these variables. What about deacceleration?

Deacceleration is just the inverse of acceleration, you multiply it by -1

So is this right? For Acceleration

local Mass = GetMass(LocalPlayerCharacter)
local Force = Mass * 5
local Acceleration = Force / Mass

while wait(1) do
	LocalPlayerHumanoid.WalkSpeed = LocalPlayerHumanoid.WalkSpeed + Acceleration
end

It’s almost right, Acceleration is rate of change in speed over time. So it’s a bit more complicated. You need to have a variable for speed, then multiply by the deltatime which is derived from the .Stepped or .RenderStepped function of RunService (I Think)

I don’t see why you would want to simulate acceleration via scripts though, Are you trying to make a realistic sort of sprint script?

I’m making it for my cars. But if the starting speed for the cars is 0 then the rate change in speed would be 0. I want it to be like Jailbreaks cars but I cant find any articles on calculating how far a car will roll.

For my car, I didn’t use acceleration, Instead I used a formula for a graph, That’s an option too but it’s not advised.

Here’s what I would do:

  • Use a BodyMover and then set a propper MaxForce value, This should simulate acceleration well enough.
  • If that doesn’t work, Make a localscript, hand network ownership to the client and then update the car at every .RenderStepped or so. Use DeltaTime and multiply it by the speed then have a variable for acceleration, Something like this:
local Acceleration = 4 -- This can be changed to make the car accelerate faster / slower

RunService.RenderStepped:Connect(function(dt)
local Speed = CarSpeed
Speed += Acceleration * dt

In that example speed just equals dt?

No, In that example: Speed = Speed + (Acceleration * dt)
dt is deltatime, it is the time passed since the last frame. so in reality it would look something like:

Speed = Speed + (4 * 0.016)

1 Like

You can do the non friction at its forwards velocity and multiply it by the acceleration