SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class MovementController : MonoBehaviour
  4. {
  5.     #region Variables
  6.     [Header("Player speed variables")]
  7.     public float movementSpeed = 3.0f;
  8.  
  9.     #region CACHING VARIABLES
  10.     Vector2 movement = new Vector2();
  11.     Animator animator;
  12.     string animationState = "AnimationState";
  13.     Rigidbody2D rb2D;
  14.     #endregion
  15.  
  16.  
  17.     public enum CharStates
  18.     {
  19.         walkEast = 1,
  20.         walkSouth = 2,
  21.         walkWest = 3,
  22.         walkNorth = 4,
  23.         idleSouth = 5
  24.     }
  25.  
  26.     [Header("Character state")]
  27.     public CharStates characterState = CharStates.idleSouth; // now you can see your state at inspector.
  28.     #endregion
  29.  
  30.  
  31.     private void Start()
  32.     {
  33.         animator = GetComponent<Animator>();
  34.         rb2D = GetComponent<Rigidbody2D>();
  35.     }
  36.  
  37.     // Update usually used for Inputs.
  38.     private void Update()
  39.     {
  40.         AnimationUpdate();
  41.         MoveCharacter();
  42.     }
  43.  
  44.     //Always make sure anything related to physics should be here. Input at UPDATE().
  45.     void FixedUpdate()
  46.     {
  47.         //Caching variables will always make it easier. Just for organization, you can change it back.
  48.         Vector2 velocityChanging = new Vector2(
  49.             Input.GetAxis("Horizontal") * movementSpeed,
  50.             Input.GetAxis("Vertical") * movementSpeed
  51.          );
  52.         rb2D.velocity = velocityChanging;
  53.     }
  54.  
  55.     //Since this is Input, we place this function at UPDATE().
  56.     private void MoveCharacter()
  57.     {
  58.         movement.x = Input.GetAxisRaw("Horizontal");
  59.         movement.y = Input.GetAxisRaw("Vertical");
  60.     }
  61.  
  62.     //Setting animations, this is upto you if you want it to be fixedupdate or update.
  63.     private void AnimationUpdate()
  64.     {
  65.         if (movement.x > 0)
  66.         {
  67.             animator.SetInteger(animationState, (int)CharStates.walkEast);
  68.         }
  69.         else if (movement.x < 0)
  70.         {
  71.             animator.SetInteger(animationState, (int)CharStates.walkWest);
  72.         }
  73.         else if (movement.y > 0)
  74.         {
  75.             animator.SetInteger(animationState, (int)CharStates.walkNorth);
  76.         }
  77.         else if (movement.y < 0)
  78.         {
  79.             animator.SetInteger(animationState, (int)CharStates.walkSouth);
  80.         }
  81.         else
  82.         {
  83.             animator.SetInteger(animationState, (int)CharStates.idleSouth);
  84.         }
  85.     }
  86. }
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