How to make an animation smoothly transition back?

I’m making a running system w/ stamina for my game, and I have a custom animation for running. The problem is, I don’t know how to make the animation smoothly stop & transition back when it’s finished.

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Running = true
		Character.Humanoid.WalkSpeed = Character.Humanoid.WalkSpeed*2
		while Stamina > 0 and Running do
			Stamina -= 1
			--print(Stamina)
			if Stamina == 0 then
				script.Parent.StaminaBar.Bar.Visible = false
			else
				script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			end
			wait()
			if Stamina == 0 then
				Character.Humanoid.WalkSpeed = WalkSpeed
			end
		end
	end 				-- scripts for this game by @rcw#9877
end)

UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Running = false
		Character.Humanoid.WalkSpeed = WalkSpeed
		while Stamina < 100 and not Running do
			script.Parent.StaminaBar.Bar.Visible = true
			Stamina += 1
			--print(Stamina)
			script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			task.wait()
			if Stamina == 0 then
				Character.Humanoid.WalkSpeed = WalkSpeed
			end
		end
	end
end)

If anyone could help I would appreciate it!

2 Likes

While I can’t really provide you with a script here, I can tell you something that you can try.
You can fork and edit the animation script under your character so that when the player stops the input, it doesn’t immediately cancel out the animation and rather just slowly transitions into the idle one.

1 Like

you can try using the :AdjustWeight property like so:

AnimationTrack:AdjustWeight(1)

or

AnimationTrack:AdjustWeight(Animation.Lengh / 2)

AnimationTrack:Play() and AnimationTrack:Stop() have a fadeTime parameter. You may be able to utilize those to make smooth transitions.

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