Illia_R

Player Animation

Nov 20th, 2020 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. //Player Animation
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PlayerAnimation : MonoBehaviour
  7. {
  8.     private Animator animator;
  9.     private CharacterController _controller;
  10.     private bool _isGrounded;
  11.     public void Start()
  12.     {
  13.         _controller = GetComponent<CharacterController>();
  14.         animator = GetComponent<Animator>();
  15.     }
  16.  
  17.     public void Update()
  18.     {
  19.         _isGrounded = _controller.isGrounded;
  20.         bool isWalking = animator.GetBool("isWalking");
  21.         bool isRunning = animator.GetBool("isRunning");
  22.  
  23.         bool leftPressed = Input.GetKey(KeyCode.A);
  24.         bool rightPressed = Input.GetKey(KeyCode.D);
  25.         bool runPressed = Input.GetKey(KeyCode.X);
  26.         bool jumpPressed = Input.GetKeyDown(KeyCode.Space);
  27.  
  28.  
  29.         OnStateEnter(); // TODO: check and make it better
  30.  
  31.         if ((leftPressed || rightPressed) && !isWalking)
  32.         {
  33.             animator.SetBool("isWalking", true);
  34.         }
  35.  
  36.         if (!(leftPressed || rightPressed) && isWalking)
  37.         {
  38.             animator.SetBool("isWalking", false);
  39.         }
  40.  
  41.         if (((leftPressed || rightPressed) && runPressed) && !isRunning)
  42.         {
  43.             animator.SetBool("isRunning", true);
  44.         }
  45.  
  46.         if ((!(leftPressed || rightPressed) || !runPressed) && isRunning)
  47.         {
  48.             animator.SetBool("isRunning", false);
  49.         }
  50.        
  51.         if (jumpPressed && _isGrounded)
  52.         {
  53.             animator.SetTrigger("Jumping");
  54.         }
  55.     }
  56.  
  57.     private void OnStateEnter()
  58.     {
  59.         animator.ResetTrigger("Jumping");
  60.     }
  61. }
  62.  
Add Comment
Please, Sign In to add comment