Advertisement
SvOzMaS

ZombieAnimatorController

Dec 13th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(Animator))]
  5. public class ZombieAnimationController : MonoBehaviour {
  6.  
  7.     [HideInInspector]
  8.     public Animator anim;                           //Character animator controller reference
  9.     private float hSpeed;                           //Animator parameter hSpeed
  10.     private float animationSpeedSmoothing;
  11.     [Range(0.1f,1)]
  12.     public float walkToRunBlending;                // Percentage of the maximun agent speed to blend between walk and run
  13.  
  14.     // Use this for initialization
  15.     void Start () {
  16.         //Setting up references
  17.         anim = GetComponent<Animator>();
  18.     }
  19.    
  20.     public void motionAnimationsHandler(float currentSpeed, float maxSpeed) {
  21.         //idle
  22.         if (currentSpeed <= 0.1) {
  23.             blendMotionAnimations(0f, 0.5f);
  24.         }
  25.         //walk
  26.         else if (currentSpeed > 0.1 && currentSpeed < maxSpeed * walkToRunBlending) {
  27.             blendMotionAnimations(0.5f, 0.5f);
  28.         }
  29.         //run
  30.         else if (currentSpeed >= maxSpeed * walkToRunBlending) {
  31.             blendMotionAnimations(1f, 0.5f);
  32.         }
  33.     }
  34.  
  35.     private void blendMotionAnimations(float targetSpeed, float acceleration) {
  36.         hSpeed = Mathf.SmoothDamp(hSpeed, targetSpeed, ref animationSpeedSmoothing, acceleration);
  37.         // Update motion animation parameters
  38.         anim.SetFloat("hSpeed", hSpeed);
  39.     }
  40.  
  41.     public void triggerAttackAnimation(int attackType = -1) {
  42.  
  43.         if (attackType == -1) {
  44.             //randomize the attack type
  45.             attackType = Random.Range(0, 7);
  46.         }
  47.  
  48.         anim.SetFloat("attackType", attackType);
  49.         anim.SetTrigger("attack");
  50.     }
  51.  
  52.     public void triggerRandomBehaviourAnimation(int behaviourType = -1) {
  53.  
  54.         if (behaviourType == -1) {
  55.             //randomize the attack type
  56.             behaviourType = Random.Range(0, 3);
  57.         }
  58.  
  59.         anim.SetFloat("randomBehaviourType", behaviourType);
  60.         anim.SetTrigger("randomBehaviour");
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement