Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class UnitychanSpriteAction : MonoBehaviour
- {
- static int hashSpeed = Animator.StringToHash ("Speed");
- static int hashFallSpeed = Animator.StringToHash ("FallSpeed");
- static int hashGroundDistance = Animator.StringToHash ("GroundDistance");
- static int hashIsCrouch = Animator.StringToHash ("IsCrouch");
- static int hashAttack1 = Animator.StringToHash ("Attack1");
- static int hashAttack2 = Animator.StringToHash ("Attack2");
- static int hashAttack3 = Animator.StringToHash ("Attack3");
- static int hashDamage = Animator.StringToHash ("Damage");
- static int hashIsDead = Animator.StringToHash ("IsDead");
- [SerializeField] private float characterHeightOffset = 0.2f;
- [SerializeField] LayerMask groundMask;
- [SerializeField, HideInInspector] Animator animator;
- [SerializeField, HideInInspector]SpriteRenderer spriteRenderer;
- [SerializeField, HideInInspector]Rigidbody2D rig2d;
- public int hp = 4;
- public Rigidbody2D Rig2d { get => rig2d; set => rig2d = value; }
- [SerializeField]
- private List<RuntimeAnimatorController> animatorControllerList = new List<RuntimeAnimatorController>();
- static int hashStateWarmingup = Animator.StringToHash("Warming.WarmingUp");
- void Awake ()
- {
- animator = GetComponent<Animator> ();
- spriteRenderer = GetComponent<SpriteRenderer> ();
- Rig2d = GetComponent<Rigidbody2D> ();
- }
- void Update ()
- {
- float axis = Input.GetAxisRaw ("Horizontal");
- bool isDown = Input.GetAxisRaw ("Vertical") < 0;
- if (Input.GetButtonDown ("Jump")) {
- Rig2d.velocity = new Vector2 (Rig2d.velocity.x, 5);
- }
- var distanceFromGround = Physics2D.Raycast (transform.position, Vector3.down, 1, groundMask);
- // update animator parameters
- animator.SetBool (hashIsCrouch, isDown);
- animator.SetFloat (hashGroundDistance, distanceFromGround.distance == 0 ? 99 : distanceFromGround.distance - characterHeightOffset);
- animator.SetFloat (hashFallSpeed, Rig2d.velocity.y);
- animator.SetFloat (hashSpeed, Mathf.Abs (axis));
- if( Input.GetKeyDown(KeyCode.Z) ){ animator.SetTrigger(hashAttack1); }
- if( Input.GetKeyDown(KeyCode.X) ){ animator.SetTrigger(hashAttack2); }
- if( Input.GetKeyDown(KeyCode.C) ){ animator.SetTrigger(hashAttack3); }
- // change attack mode
- if( Input.GetKeyDown(KeyCode.M) )
- {
- // unavailable during an attack
- if(animator.GetHashCode() == hashAttack1 ||
- animator.GetHashCode() == hashAttack2 ||
- animator.GetHashCode() == hashAttack3)
- {
- return;
- }
- // change attack mode animation
- animator.Play(hashStateWarmingup);
- // set attack mode
- if(animator.runtimeAnimatorController.Equals(animatorControllerList[0]))
- {
- animator.runtimeAnimatorController = animatorControllerList[1];
- }
- else
- {
- animator.runtimeAnimatorController = animatorControllerList[0];
- }
- }
- // flip sprite
- if (axis != 0) spriteRenderer.flipX = axis < 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement