Sprinting and walking animation based on player speed?

Yes there is, one moment let me create a script.

1 Like

I would recommend using the Player’s HumanoidRootPart as to get the amount of speed the player is accelerating at.

  local maxSpeed = MAX_WALK_SPEED; -- Maximum speed value for full animation speed
  local currentSpeed = <HumanoidRootPart>.AssemblyLinearVelocity.Magnitude;
  local normalizedSpeed = math.min(currentSpeed / maxSpeed, 1);

  -- If you want to apply a simple smoothing (ease out quad) as an example
  local smoothedSpeed = normalizedSpeed * normalizedSpeed;

  print(smoothedSpeed)

2 Likes

Ok, I believe I did it. Tell me if there are any errors on your end and then I can correct them. Same thing as before use this as a local script under StarterCharacterScripts:

local RunService = game:GetService("RunService")

local character = script.Parent 
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local MAX_WALK_SPEED = humanoid.WalkSpeed -- or any value of your choosing

local DEFAULT_RUNNING_ANIMATION = 16493330373
local DEFAULT_WALKING_ANIMATION = 16493310971

local runAnimation = Instance.new("Animation")
runAnimation.AnimationId = "rbxassetid://" .. DEFAULT_RUNNING_ANIMATION
runAnimation = animator:LoadAnimation(runAnimation)
runAnimation.Priority = Enum.AnimationPriority.Idle

local renderConnection 

local isRunning = false

local function startRun()
	if isRunning == true then
		return
	end
	
	isRunning = true
	
	runAnimation:Play()
end

local function stopRun()
	if isRunning == false then
		return
	end

	isRunning = false
	
	runAnimation:Stop()
end

local function determine(deltaTime)
	if humanoidRootPart.AssemblyLinearVelocity.Magnitude > 20 then
		startRun()
		
		local maxSpeed = MAX_WALK_SPEED -- Maximum speed value for full animation speed
		local currentSpeed = humanoidRootPart.AssemblyLinearVelocity.Magnitude
		local normalizedSpeed = math.min(currentSpeed / maxSpeed, 1)

		-- If you want to apply a simple smoothing (ease out quad) as an example
		local smoothedSpeed = normalizedSpeed * normalizedSpeed
		
		runAnimation:AdjustSpeed(smoothedSpeed)
	else
		stopRun()
	end
end

humanoid.StateChanged:Connect(function(previous, current)
	if previous == Enum.HumanoidStateType.Running then
		if renderConnection ~= nil then

			renderConnection:Disconnect()
			renderConnection = nil

			stopRun()
		end
	elseif current == Enum.HumanoidStateType.Running then
		if renderConnection == nil then
			renderConnection = RunService.RenderStepped:Connect(determine)
		end
	end
end)

character:WaitForChild("Animate"):WaitForChild("walk"):WaitForChild("WalkAnim").AnimationId = "rbxassetid://" .. DEFAULT_WALKING_ANIMATION
2 Likes

Going off of what Ace said, I think it would be very useful to add a change in animation speed as well.

1 Like

What went wrong: no walking animation anymore, When running the running animation plays but frozen in one keyframe

Heres the console although nothing came in it whenever i started/stopped running:

also yes, i think that making the run animation faster would be even better

Ah, I see the problem, one moment.

1 Like

I have just edited the script above to fix the problem and apply Ace’s contributions to the animation speed.

Let me know if there are any errors from the script, or if it does not function as intended.

1 Like

it seems the animation refresh works great, though the walking animation is now the default roblox and the running animation is the custom running animation

1 Like

My apologies, let me add a place where you can edit the custom walking animation.

1 Like

Just edited it to allow you to change the default walking animation ID. Hopefully nothing errors, if it does let me know.

1 Like

You are a legend, thank you so much my dude.

1 Like

which part exactly changes the speed of the animation when running?

runAnimation:AdjustSpeed(smoothedSpeed) is the part that changes the speed of the animation.
If you want to scale to make it faster or whatnot you could do runAnimation:AdjustSpeed(smoothedSpeed * x) x being any number you choose.

1 Like

Hey, apologies for the late comeback but I just tested the game in roblox itself, not in studio and in there no animations are loaded. only jump, whenever i walk or run my character stays just standing up straight

Where is the game published under and where are the animations published under?

1 Like

I just saw this question answered a day ago …
Here … if not … Here

The game is published under my group and the animations under my profile, should I add the animations to my group too?

I’m pretty sure that’s the problem. So yes. Tell me what happens.

1 Like

Yea, this was the problem. Thanks!

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