Created
May 25, 2025 10:13
-
-
Save adammyhre/737837b565216b463ea46e65e195a3ae to your computer and use it in GitHub Desktop.
Animation Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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