Skip to content

Instantly share code, notes, and snippets.

@adammyhre
Created May 25, 2025 10:13
Show Gist options
  • Save adammyhre/737837b565216b463ea46e65e195a3ae to your computer and use it in GitHub Desktop.
Save adammyhre/737837b565216b463ea46e65e195a3ae to your computer and use it in GitHub Desktop.
Animation Controller
using ImprovedTimers;
using UnityEngine;
namespace AdvancedController {
[RequireComponent(typeof(PlayerController))]
public class AnimationController : MonoBehaviour {
PlayerController controller;
Animator animator;
CountdownTimer animationTimer;
readonly int locomotionHash = Animator.StringToHash("Locomotion");
readonly int speedHash = Animator.StringToHash("Speed");
readonly int isJumpingHash = Animator.StringToHash("IsJumping");
void Start() {
controller = GetComponent<PlayerController>();
animator = GetComponentInChildren<Animator>();
controller.OnJump += HandleJump;
controller.OnLand += HandleLand;
animationTimer = new CountdownTimer(0f);
}
void Update() {
animator.SetFloat(speedHash, controller.GetMovementVelocity().magnitude);
}
void HandleJump(Vector3 momentum) => animator.SetBool(isJumpingHash, true);
void HandleLand(Vector3 momentum) => animator.SetBool(isJumpingHash, false);
public void PlayOneShot(AnimationClip clip) {
if (clip == null || animator == null) return;
Debug.Log($"Playing one-shot animation: {clip.name}");
animationTimer.OnTimerStop = () => animator.CrossFade(locomotionHash, 0.1f);
animationTimer.Reset(clip.length);
animationTimer.Start();
animator.CrossFade(clip.name, 0.1f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment