GibTreaty

2D Player Movement

May 3rd, 2015
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.02 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class Player : MonoBehaviour {
  5.  
  6.     public float speed = 2;
  7.     public float jumpSpeed = 2;
  8.     public float maxSpeed = 5;
  9.  
  10.     public float dragOutput;
  11.     public float constantDragOutput;
  12.  
  13.     public AnimationCurve dragCurve;
  14.     public float dragBelowInput = .1f;
  15.     public AnimationCurve constantDragCurve;
  16.  
  17.     public Transform weaponSlot;
  18.     public Transform leftArm;
  19.     public Transform rightArm;
  20.     public Transform head;
  21.     public Transform graphics;
  22.  
  23.     public Transform groundRaycaster;
  24.     public Transform feetLocationTransform;
  25.     public float raycastDistance = .1f;
  26.  
  27.     float moveInput;
  28.     float jumpInput;
  29.  
  30.     Vector2 _velocity;
  31.  
  32.     Animator AnimatorComponent { get; set; }
  33.     public bool IsGrounded { get; private set; }
  34.     Rigidbody2D Rigidbody2DComponent { get; set; }
  35.     public Vector2 Velocity {
  36.         get { return _velocity; }
  37.         private set { _velocity = value; }
  38.     }
  39.  
  40.     Dictionary<Collider2D, int> colliderLayers = new Dictionary<Collider2D, int>();
  41.  
  42.     void OnEnable() {
  43.         AnimatorComponent = GetComponent<Animator>();
  44.         Rigidbody2DComponent = GetComponent<Rigidbody2D>();
  45.  
  46.         //foreach(Collider2D collider in GetComponentsInChildren<Collider2D>())
  47.         //  colliderLayers[collider] = collider.gameObject.layer;
  48.     }
  49.  
  50.     void Update() {
  51.         dragOutput = dragCurve.Evaluate(Mathf.Abs(Rigidbody2DComponent.velocity.x));
  52.         constantDragOutput = constantDragCurve.Evaluate(Mathf.Abs(Rigidbody2DComponent.velocity.x));
  53.  
  54.         AnimatorComponent.SetFloat("Speed", Mathf.Abs(Rigidbody2DComponent.velocity.x));
  55.  
  56.         DetectDrag();
  57.         DetectGround();
  58.         DetectJump();
  59.     }
  60.  
  61.     void LateUpdate() {
  62.         Vector2 direction = (weaponSlot.position - leftArm.position).normalized;
  63.         leftArm.eulerAngles = new Vector3(0, 0, Mathf.Atan2(-direction.y, -direction.x) * Mathf.Rad2Deg + 180);
  64.  
  65.         direction = (weaponSlot.position - rightArm.position).normalized;
  66.         rightArm.eulerAngles = new Vector3(0, 0, Mathf.Atan2(-direction.y, -direction.x) * Mathf.Rad2Deg + 180);
  67.  
  68.         direction = (weaponSlot.position - head.position).normalized;
  69.         float horizontalScale = Mathf.Sign(graphics.localScale.x);
  70.         head.eulerAngles = new Vector3(0, 0, Mathf.Clamp(Mathf.Atan2(-direction.x * horizontalScale, direction.y) * Mathf.Rad2Deg + 90, -80, 60));
  71.     }
  72.  
  73.     void FixedUpdate() {
  74.         Vector3 velocity = Rigidbody2DComponent.velocity;
  75.  
  76.         if(IsGrounded)
  77.             if(Mathf.Abs(moveInput) > .1f) {
  78.                 if(Mathf.Sign(moveInput) > 0) {
  79.                     if(velocity.x < 0) velocity.x = 0;
  80.                 }
  81.                 else {
  82.                     if(velocity.x > 0) velocity.x = 0;
  83.                 }
  84.  
  85.                 Rigidbody2DComponent.velocity = velocity;
  86.             }
  87.  
  88.         //Debug.Log((Mathf.InverseLerp(-maxSpeed * 2, maxSpeed * 2, Rigidbody2DComponent.velocity.x) - .5f) * 2);
  89.  
  90.  
  91.         Rigidbody2DComponent.AddForce(Vector2.right * moveInput * speed);
  92.     }
  93.  
  94.     void OnDrawGizmos() {
  95.         Gizmos.color = Color.green;
  96.         Gizmos.DrawRay(groundRaycaster.position, Vector3.down * raycastDistance);
  97.     }
  98.  
  99.     void DetectDrag() {
  100.         Vector2 velocity = Rigidbody2DComponent.velocity;
  101.  
  102.         if(IsGrounded && Mathf.Abs(moveInput) < dragBelowInput)
  103.             velocity.x -= velocity.x * Time.deltaTime * dragCurve.Evaluate(Mathf.Abs(velocity.x));
  104.  
  105.         velocity.x -= velocity.x * Time.deltaTime * constantDragCurve.Evaluate(Mathf.Abs(velocity.x));
  106.  
  107.         Rigidbody2DComponent.velocity = velocity;
  108.     }
  109.  
  110.     void DetectGround() {
  111.         //foreach(KeyValuePair<Collider2D, int> colliderLayer in colliderLayers)
  112.         //  colliderLayer.Key.gameObject.layer = Physics2D.IgnoreRaycastLayer;
  113.  
  114.         RaycastHit2D hit = Physics2D.Raycast(groundRaycaster.position, -Vector2.up, raycastDistance);
  115.  
  116.         if(hit.collider) {
  117.             if(Rigidbody2DComponent.velocity.y <= 0) {
  118.                 Rigidbody2DComponent.gravityScale = 0;
  119.  
  120.                 Vector3 position = transform.position;
  121.                 position.y = position.y - (feetLocationTransform.position.y - hit.point.y);
  122.                 transform.position = position;
  123.             }
  124.  
  125.             IsGrounded = true;
  126.         }
  127.         else {
  128.             Rigidbody2DComponent.gravityScale = 1;
  129.             IsGrounded = false;
  130.         }
  131.  
  132.         //foreach(KeyValuePair<Collider2D, int> colliderLayer in colliderLayers)
  133.         //  colliderLayer.Key.gameObject.layer = colliderLayer.Value;
  134.     }
  135.  
  136.     void DetectJump() {
  137.         if(jumpInput > .5f) {
  138.             if(IsGrounded && Rigidbody2DComponent.velocity.y <= 0) {
  139.                 Vector3 velocity = Rigidbody2DComponent.velocity;
  140.                 velocity.y = jumpSpeed;
  141.                 Rigidbody2DComponent.velocity = velocity;
  142.             }
  143.         }
  144.     }
  145.  
  146.     public void Attack() {
  147.         IWeapon weapon = weaponSlot.GetComponentInChildren<IWeapon>();
  148.  
  149.         if(weapon != null) weapon.Attack();
  150.     }
  151.  
  152.     public void MoveInput(float speed) {
  153.         moveInput = Mathf.Clamp(speed, -1, 1);
  154.     }
  155.  
  156.     public void JumpInput(float jump) {
  157.         jumpInput = jump;
  158.     }
  159.  
  160.     public void SetWeaponAngle(float angle) {
  161.         bool up = Mathf.Abs(180 - (angle)) >= 90;
  162.  
  163.         graphics.localScale = new Vector3(up ? 1 : -1, graphics.localScale.y);
  164.         leftArm.localScale = new Vector3(leftArm.localScale.x, up ? 1 : -1);
  165.         rightArm.localScale = new Vector3(rightArm.localScale.x, up ? 1 : -1);
  166.  
  167.         leftArm.GetComponentInChildren<SpriteRenderer>().sortingOrder = up ? 2 : -2;
  168.         rightArm.GetComponentInChildren<SpriteRenderer>().sortingOrder = up ? -2 : 2;
  169.  
  170.         weaponSlot.parent.localEulerAngles = new Vector3(0, 0, angle);
  171.     }
  172.     public void SetWeaponAngle(Vector2 direction) {
  173.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 180;
  174.  
  175.         SetWeaponAngle(angle);
  176.     }
  177.  
  178.     public void SetWeapon(Transform weapon) {
  179.         foreach(Transform currentWeapon in weaponSlot) {
  180.             currentWeapon.SetParent(null);
  181.  
  182.             Rigidbody2D rigidbody2D = currentWeapon.GetComponent<Rigidbody2D>();
  183.             rigidbody2D.isKinematic = false;
  184.             rigidbody2D.velocity = Rigidbody2DComponent.velocity;
  185.  
  186.             foreach(Collider2D collider in currentWeapon.GetComponentsInChildren<Collider2D>(true))
  187.                 collider.enabled = true;
  188.         }
  189.  
  190.         if(weapon) {
  191.             weapon.GetComponent<Rigidbody2D>().isKinematic = true;
  192.             weapon.SetParent(weaponSlot, false);
  193.             weapon.localPosition = Vector3.zero;
  194.             weapon.localRotation = Quaternion.identity;
  195.  
  196.             foreach(Collider2D collider in weapon.GetComponentsInChildren<Collider2D>(true))
  197.                 collider.enabled = false;
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment