Advertisement
GoodNoodle

Anim Manager

Jul 11th, 2021
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6.  
  7.     public class AnimHandeler : MonoBehaviour
  8.     {
  9.         PlayerManager playerManager;
  10.         public Animator anim;
  11.          InputHandeler inputHandler;
  12.          PlayerMovement playerMovement;
  13.         int vertical;
  14.         int horizontal;
  15.         public bool canRotate;
  16.  
  17.         public void Initialize()
  18.         {
  19.             playerManager = GetComponentInParent<PlayerManager>();
  20.             anim = GetComponent<Animator>();
  21.             inputHandler = GetComponentInParent<InputHandeler>();
  22.             playerMovement = GetComponentInParent<PlayerMovement>();
  23.             vertical = Animator.StringToHash("Vertical");
  24.             horizontal = Animator.StringToHash("Horizontal");
  25.         }
  26.     public void attack1()
  27.     {
  28.         anim.SetTrigger(AnimTags.attack1Trigger);
  29.     }
  30.     public void attack2()
  31.     {
  32.         anim.SetTrigger(AnimTags.attack2Trigger);
  33.     }
  34.     public void attack3()
  35.     {
  36.         anim.SetTrigger(AnimTags.attack3Trigger);
  37.  
  38.     }
  39.     public void HeavyAttack1()
  40.     {
  41.         anim.SetTrigger(AnimTags.Heavyattack1Trigger);
  42.     }
  43.     public void HeavyAttack2()
  44.     {
  45.         anim.SetTrigger(AnimTags.Heavyattack2Trigger);
  46.     }
  47.  
  48.     public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement, bool isSprinting)
  49.         {
  50.             #region Vertical
  51.             float v = 0;
  52.  
  53.             if (verticalMovement > 0 && verticalMovement < 0.55f)
  54.             {
  55.                 v = 0.5f;
  56.             }
  57.             else if (verticalMovement > 0.55f)
  58.             {
  59.                 v = 1;
  60.             }
  61.             else if (verticalMovement < 0 && verticalMovement > -0.55f)
  62.             {
  63.                 v = -0.5f;
  64.             }
  65.             else if (verticalMovement < -0.55f)
  66.             {
  67.                 v = -1;
  68.             }
  69.             else
  70.             {
  71.                 v = 0;
  72.             }
  73.             #endregion
  74.  
  75.             #region Horizontal
  76.             float h = 0;
  77.  
  78.             if (horizontalMovement > 0 && horizontalMovement < 0.55f)
  79.             {
  80.                 h = 0.5f;
  81.             }
  82.             else if (horizontalMovement > 0.55f)
  83.             {
  84.                 h = 1;
  85.             }
  86.             else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
  87.             {
  88.                 h = -0.5f;
  89.             }
  90.             else if (horizontalMovement < -0.55f)
  91.             {
  92.                 h = -1;
  93.             }
  94.             else
  95.             {
  96.                 h = 0;
  97.             }
  98.             #endregion
  99.  
  100.             if(isSprinting)
  101.         {
  102.             v = 2;
  103.             h = horizontalMovement;
  104.         }
  105.  
  106.             anim.SetFloat(vertical, v, 0.1f, Time.deltaTime);
  107.             anim.SetFloat(horizontal, h, 0.1f, Time.deltaTime);
  108.         }
  109.  
  110.         public void CanRotate()
  111.         {
  112.             canRotate = true;
  113.         }
  114.  
  115.         public void StopRotation()
  116.         {
  117.             canRotate = false;
  118.         }
  119.  
  120.         public void PlayTargetAnim(string tarAnim, bool isInteracting)
  121.         {
  122.             anim.applyRootMotion = isInteracting;
  123.             anim.SetBool("IsInteracting", isInteracting);
  124.             anim.CrossFade(tarAnim, 0.2f);
  125.         }
  126.  
  127.     public void OnAnimatorMove()
  128.     {
  129.         if (playerManager.isInterationg == false) return;
  130.  
  131.         float delta = Time.deltaTime;
  132.         playerMovement.rigidbody.drag = 0;
  133.         Vector3 deltaPos = anim.deltaPosition;
  134.         deltaPos.y = 0;
  135.         Vector3 velocity = deltaPos / delta;
  136.         playerMovement.rigidbody.velocity = velocity;
  137.        
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement