Changed FPS changes the speed

I wrote code of decreasing velocity of LinearVelocity, it works almost correctly but the problem is the speed of decreasing velocity changes(depending on FPS) so it makes LinearVelocity acting further which isnt supposed to be.

My code:

				local max_power = (dir == "left" and Vector3.new(-80, 0, 0)) or (dir == "right" and Vector3.new(80, 0, 0))
				local LinearVelocity = Instance.new("LinearVelocity", char:FindFirstChild("HumanoidRootPart"))
				LinearVelocity.Attachment0 = char:FindFirstChild("HumanoidRootPart"):FindFirstChild("RootAttachment")
				LinearVelocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
				LinearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
				LinearVelocity.ForceLimitMode = Enum.ForceLimitMode.PerAxis
				LinearVelocity.MaxAxesForce = Vector3.new(30000, 0, 30000)
				LinearVelocity.VectorVelocity = max_power
				game.Debris:AddItem(LinearVelocity, 0.3)
				table.insert(temp, LinearVelocity) -- adds variable to global variable to clean for exception

				table.insert(temp, task.spawn(function()
					local time_decreasing = 0.3
					local percents = 1

					local timer = tick()
					repeat
						game:GetService("RunService").Heartbeat:Wait()
						LinearVelocity.VectorVelocity = Vector3.new(max_power.X*percents, max_power.Y*percents, max_power.Z*percents)
						if (percents-0.01) > 0 then
							percents-=0.01
						else
							percents = 0
						end
						print(percents)
					until (LinearVelocity.Enabled == false or LinearVelocity == nil or tick() - timer >= time_decreasing) and percents <= 0
				end))

You need to multiply the percentage by the delta time returned by RunService.Heartbeat:Wait() to make the velocity framerate independent


@Yeran123412 Using what I mentioned above, this should fix the problem:

local max_power = (dir == "left" and Vector3.new(-80, 0, 0)) or (dir == "right" and Vector3.new(80, 0, 0))
				local LinearVelocity = Instance.new("LinearVelocity", char:FindFirstChild("HumanoidRootPart"))
				LinearVelocity.Attachment0 = char:FindFirstChild("HumanoidRootPart"):FindFirstChild("RootAttachment")
				LinearVelocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
				LinearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
				LinearVelocity.ForceLimitMode = Enum.ForceLimitMode.PerAxis
				LinearVelocity.MaxAxesForce = Vector3.new(30000, 0, 30000)
				LinearVelocity.VectorVelocity = max_power
				game.Debris:AddItem(LinearVelocity, 0.3)
				table.insert(temp, LinearVelocity) -- adds variable to global variable to clean for exception

				table.insert(temp, task.spawn(function()
					local time_decreasing = 0.3
					local percents = 1

					local timer = tick()
					repeat
						local deltaTime = game:GetService("RunService").Heartbeat:Wait()
						LinearVelocity.VectorVelocity = Vector3.new(max_power.X*percents, max_power.Y*percents, max_power.Z*percents)

						percents = math.max(percents - (0.01 * deltaTime), 0) -- Using math.max so the value never goes below 0
						print(percents)
					until (LinearVelocity.Enabled == false or LinearVelocity == nil or tick() - timer >= time_decreasing) and percents == 0
				end))
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.