Advertisement
shadowx320

AnimatorHandler

Mar 19th, 2021
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace SG
  6. {
  7.     public class AnimatorHandler : MonoBehaviour
  8.     {
  9.         public Animator anim;
  10.         public InputHandler inputHandler;
  11.         public PlayerLocomotion playerLocomotion;
  12.         int vertical;
  13.         int horizontal;
  14.         public bool canRotate;
  15.  
  16.         public void Initialize()
  17.         {
  18.             anim = GetComponent<Animator>();
  19.             inputHandler = GetComponentInParent<InputHandler>();
  20.             playerLocomotion = GetComponentInParent<PlayerLocomotion>();
  21.             vertical = Animator.StringToHash("Vertical");
  22.             horizontal = Animator.StringToHash("Horizontal");
  23.         }
  24.  
  25.         public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement)
  26.         {
  27.             #region Vertical
  28.             float v = 0;
  29.  
  30.             if (verticalMovement > 0 && verticalMovement < 0.55f)
  31.             {
  32.                 v = 0.5f;
  33.             }
  34.             else if (verticalMovement > 0.55f)
  35.             {
  36.                 v = 1;
  37.             }
  38.             else if (verticalMovement < 0 && verticalMovement > -0.55f)
  39.             {
  40.                 v = -0.5f;
  41.             }
  42.             else if (verticalMovement < -0.55f)
  43.             {
  44.                 v = -1;
  45.             }
  46.             else
  47.             {
  48.                 v = 0;
  49.             }
  50.             #endregion
  51.  
  52.             #region Horizontal
  53.             float h = 0;
  54.  
  55.             if (horizontalMovement > 0 && horizontalMovement < 0.55f)
  56.             {
  57.                 h = 0.5f;
  58.             }
  59.             else if (verticalMovement > 0.55f)
  60.             {
  61.                 h = 1;
  62.             }
  63.             else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
  64.             {
  65.                 h = -0.5f;
  66.             }
  67.             else if (horizontalMovement < -0.55f)
  68.             {
  69.                 h = -1;
  70.             }
  71.             else
  72.             {
  73.                 h = 0;
  74.             }
  75.             #endregion
  76.  
  77.             anim.SetFloat(vertical, v, 0.1f, Time.deltaTime);
  78.             anim.SetFloat(horizontal, h, 0.1f, Time.deltaTime);
  79.         }
  80.  
  81.         public void PlayTargetAnimation(string targetAnim, bool isInteracting)
  82.         {
  83.             anim.applyRootMotion = isInteracting;
  84.             anim.SetBool("isInteracting", isInteracting);
  85.             anim.CrossFade(targetAnim, 0.2f);
  86.         }
  87.  
  88.         public void CanRotate()
  89.         {
  90.             canRotate = true;
  91.         }
  92.  
  93.         public void StopRotation()
  94.         {
  95.             canRotate = false;
  96.         }
  97.  
  98.         private void OnAnimatorMove()
  99.         {
  100.             if (inputHandler.isInteracting == false)
  101.                 return;
  102.  
  103.             float delta = Time.deltaTime;
  104.             playerLocomotion.rigidbody.drag = 0;
  105.             Vector3 deltaPosition = anim.deltaPosition;
  106.             deltaPosition.y = 0;
  107.             Vector3 velocity = deltaPosition / delta;
  108.             playerLocomotion.rigidbody.velocity = velocity;
  109.         }
  110.  
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement