SHARE
TWEET

Untitled

a guest Jul 21st, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class UnitychanSpriteAction : MonoBehaviour
  6. {
  7.     static int hashSpeed = Animator.StringToHash ("Speed");
  8.     static int hashFallSpeed = Animator.StringToHash ("FallSpeed");
  9.     static int hashGroundDistance = Animator.StringToHash ("GroundDistance");
  10.     static int hashIsCrouch = Animator.StringToHash ("IsCrouch");
  11.     static int hashAttack1 = Animator.StringToHash ("Attack1");
  12.     static int hashAttack2 = Animator.StringToHash ("Attack2");
  13.     static int hashAttack3 = Animator.StringToHash ("Attack3");
  14.  
  15.  
  16.     static int hashDamage = Animator.StringToHash ("Damage");
  17.     static int hashIsDead = Animator.StringToHash ("IsDead");
  18.  
  19.     [SerializeField] private float characterHeightOffset = 0.2f;
  20.     [SerializeField] LayerMask groundMask;
  21.  
  22.     [SerializeField, HideInInspector] Animator animator;
  23.     [SerializeField, HideInInspector]SpriteRenderer spriteRenderer;
  24.     [SerializeField, HideInInspector]Rigidbody2D rig2d;
  25.  
  26.     public int hp = 4;
  27.  
  28.     public Rigidbody2D Rig2d { get => rig2d; set => rig2d = value; }
  29.  
  30.     [SerializeField]
  31.     private List<RuntimeAnimatorController> animatorControllerList = new List<RuntimeAnimatorController>();
  32.  
  33.     static int hashStateWarmingup = Animator.StringToHash("Warming.WarmingUp");
  34.  
  35.     void Awake ()
  36.     {
  37.         animator = GetComponent<Animator> ();
  38.         spriteRenderer = GetComponent<SpriteRenderer> ();
  39.         Rig2d = GetComponent<Rigidbody2D> ();
  40.     }
  41.  
  42.     void Update ()
  43.     {
  44.         float axis = Input.GetAxisRaw ("Horizontal");
  45.         bool isDown = Input.GetAxisRaw ("Vertical") < 0;
  46.  
  47.         if (Input.GetButtonDown ("Jump")) {
  48.             Rig2d.velocity = new Vector2 (Rig2d.velocity.x, 5);
  49.         }
  50.  
  51.         var distanceFromGround = Physics2D.Raycast (transform.position, Vector3.down, 1, groundMask);
  52.  
  53.         // update animator parameters
  54.         animator.SetBool (hashIsCrouch, isDown);
  55.         animator.SetFloat (hashGroundDistance, distanceFromGround.distance == 0 ? 99 : distanceFromGround.distance - characterHeightOffset);
  56.         animator.SetFloat (hashFallSpeed, Rig2d.velocity.y);
  57.         animator.SetFloat (hashSpeed, Mathf.Abs (axis));
  58.         if( Input.GetKeyDown(KeyCode.Z) ){  animator.SetTrigger(hashAttack1); }
  59.         if( Input.GetKeyDown(KeyCode.X) ){  animator.SetTrigger(hashAttack2); }
  60.         if( Input.GetKeyDown(KeyCode.C) ){  animator.SetTrigger(hashAttack3); }
  61.  
  62.         // change attack mode
  63.         if( Input.GetKeyDown(KeyCode.M) )
  64.         {
  65.             // unavailable during an attack
  66.             if(animator.GetHashCode() == hashAttack1 ||
  67.             animator.GetHashCode() == hashAttack2 ||
  68.             animator.GetHashCode() == hashAttack3)
  69.             {
  70.                 return;
  71.             }
  72.  
  73.             // change attack mode animation
  74.             animator.Play(hashStateWarmingup);
  75.  
  76.             // set attack mode
  77.             if(animator.runtimeAnimatorController.Equals(animatorControllerList[0]))
  78.             {
  79.                 animator.runtimeAnimatorController = animatorControllerList[1];
  80.             }
  81.             else
  82.             {
  83.                 animator.runtimeAnimatorController = animatorControllerList[0];
  84.             }
  85.         }
  86.  
  87.         // flip sprite
  88.         if (axis != 0) spriteRenderer.flipX = axis < 0;
  89.     }
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top