Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Animator))]
- public class SimpleNPCAnimator : MonoBehaviour
- {
- public AnimationClip Idle;
- public AnimationClip Walk;
- [Header("Random")]
- public bool PlayRandomIdle;
- public Vector2 RandomRange;
- public AnimationClip RandomIdle;
- [Header("Talk")]
- public bool PlayTalkAnimation;
- public AnimationClip OnTalkTo;
- private Animator anim;
- private AnimatorOverrideController clips;
- private float randomTimer;
- public void Awake()
- {
- anim = GetComponent<Animator>();
- clips = new AnimatorOverrideController(anim.runtimeAnimatorController);
- anim.runtimeAnimatorController = clips;
- if (Idle != null) clips["idle_1"] = Idle;
- if (Walk != null) clips["walking_1"] = Walk;
- if (OnTalkTo != null) clips["interact"] = OnTalkTo;
- if (RandomIdle != null) clips["idle_2"] = RandomIdle;
- randomTimer = RandomRange.y;
- }
- private void Update()
- {
- if (PlayRandomIdle)
- {
- if (randomTimer <= 0)
- {
- randomTimer = Random.Range(RandomRange.x, RandomRange.y);
- anim.SetTrigger("RandomIdle");
- }
- else
- {
- randomTimer -= Time.deltaTime;
- }
- }
- }
- public void OnInteract()
- {
- if (PlayTalkAnimation)
- {
- randomTimer = RandomRange.y;
- anim.SetTrigger("OnInteract");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement