Advertisement
Sabotender

Simple NPC Animator

Jan 22nd, 2022
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Animator))]
  4. public class SimpleNPCAnimator : MonoBehaviour
  5. {
  6.     public AnimationClip Idle;
  7.     public AnimationClip Walk;
  8.  
  9.     [Header("Random")]
  10.     public bool PlayRandomIdle;
  11.     public Vector2 RandomRange;
  12.     public AnimationClip RandomIdle;
  13.  
  14.  
  15.     [Header("Talk")]
  16.     public bool PlayTalkAnimation;        
  17.     public AnimationClip OnTalkTo;
  18.  
  19.     private Animator anim;
  20.     private AnimatorOverrideController clips;
  21.     private float randomTimer;
  22.  
  23.     public void Awake()
  24.     {
  25.         anim = GetComponent<Animator>();
  26.         clips = new AnimatorOverrideController(anim.runtimeAnimatorController);
  27.         anim.runtimeAnimatorController = clips;
  28.  
  29.         if (Idle != null) clips["idle_1"] = Idle;
  30.         if (Walk != null) clips["walking_1"] = Walk;
  31.         if (OnTalkTo != null) clips["interact"] = OnTalkTo;
  32.         if (RandomIdle != null) clips["idle_2"] = RandomIdle;
  33.  
  34.         randomTimer = RandomRange.y;
  35.     }
  36.  
  37.     private void Update()
  38.     {
  39.         if (PlayRandomIdle)
  40.         {
  41.             if (randomTimer <= 0)
  42.             {
  43.                 randomTimer = Random.Range(RandomRange.x, RandomRange.y);
  44.                 anim.SetTrigger("RandomIdle");
  45.             }
  46.             else
  47.             {
  48.                 randomTimer -= Time.deltaTime;
  49.             }
  50.         }
  51.     }
  52.  
  53.     public void OnInteract()
  54.     {
  55.         if (PlayTalkAnimation)
  56.         {
  57.             randomTimer = RandomRange.y;
  58.             anim.SetTrigger("OnInteract");
  59.         }
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement