Advertisement
Guest User

Untitled

a guest
Dec 19th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. namespace Assets.Scripts
  5. {
  6.     public enum Directions
  7.     {
  8.         Back,
  9.         Left,
  10.         Front,
  11.         Right,
  12.         Idle = -1
  13.     }
  14.  
  15.     public class PlayerMovement : MonoBehaviour
  16.     {
  17.         #region Public Members
  18.  
  19.         public float speed;
  20.         public Text debugText;
  21.  
  22.         #endregion
  23.  
  24.         #region Constants
  25.  
  26.         private const float DECAY_FACTOR = 0.85f;
  27.         private const float SPEED_FACTOR = 850f;
  28.  
  29.         #endregion
  30.  
  31.         #region Private Members
  32.  
  33.         private Rigidbody2D rb2D;
  34.         private Vector2 velocity;
  35.         private Animator animator;
  36.  
  37.         #endregion
  38.  
  39.         #region Game Loop Methods
  40.  
  41.         private void Awake()
  42.         {
  43.             animator = GetComponent<Animator>();
  44.             rb2D = GetComponent<Rigidbody2D>();
  45.         }
  46.  
  47.         private void FixedUpdate()
  48.         {
  49.             float vertical = Input.GetAxisRaw("Vertical");
  50.             float horizontal = Input.GetAxisRaw("Horizontal");
  51.             UpdateVelocity(vertical, horizontal);
  52.             UpdateAnimation();
  53.             UpdateMovment();
  54.         }
  55.  
  56.         #endregion
  57.  
  58.         #region Animation Methods
  59.  
  60.         private void UpdateAnimation()
  61.         {
  62.             Directions direction;
  63.  
  64.             if (velocity.y > 0)
  65.                 direction = Directions.Back;
  66.             else if (velocity.y < 0)
  67.                 direction = Directions.Front;
  68.             else if (velocity.x > 0)
  69.                 direction = Directions.Right;
  70.             else if (velocity.x < 0)
  71.                 direction = Directions.Left;
  72.             else
  73.                 direction = Directions.Idle;
  74.  
  75.             SetDirection(direction);
  76.         }
  77.  
  78.         private void SetDirection(Directions value)
  79.         {
  80.             animator.SetInteger("Direction", (int)value);
  81.         }
  82.  
  83.         #endregion
  84.  
  85.         #region Movement Methods
  86.  
  87.         private void UpdateMovment()
  88.         {
  89.             debugText.text = string.Format("HOR - {0}\nVER - {1}\nDIR - {2}:{3}", velocity.x, velocity.y,
  90.                 animator.GetInteger("Direction").ToString().PadLeft(2), (Directions)animator.GetInteger("Direction"));
  91.             rb2D.MovePosition(rb2D.position + velocity * Time.fixedDeltaTime);
  92.             //transform.Translate(velocity.x, velocity.y, 0f, transform);
  93.             ApplySpeedDecay();
  94.         }
  95.  
  96.         private void UpdateVelocity(float vertical, float horizontal)
  97.         {
  98.             if (vertical != 0)
  99.                 velocity.y += Mathf.Sign(vertical) * speed / SPEED_FACTOR;
  100.             if (horizontal != 0)
  101.                 velocity.x += Mathf.Sign(horizontal) * speed / SPEED_FACTOR;
  102.             animator.SetFloat("HorizontalVelocity", velocity.x / 2);
  103.             animator.SetFloat("VerticalVelocity", velocity.y / 2);
  104.         }
  105.  
  106.         private void ApplySpeedDecay()
  107.         {
  108.             // Apply speed decay
  109.             velocity.x *= DECAY_FACTOR;
  110.             velocity.y *= DECAY_FACTOR;
  111.  
  112.             // Zerofy tiny velocities
  113.             const float EPSILON = 0.1f;
  114.  
  115.             if (Mathf.Abs(velocity.x) < EPSILON)
  116.                 velocity.x = 0;
  117.             if (Mathf.Abs(velocity.y) < EPSILON)
  118.                 velocity.y = 0;
  119.         }
  120.  
  121.         #endregion
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement