Advertisement
Guest User

2D Character Controller

a guest
Aug 13th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.16 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CharController2D : MonoBehaviour {
  5.  
  6.     private Rigidbody2D rigidBody2d;
  7.     private Animator animator;
  8.     public bool facingRight = true;
  9.  
  10.     [Header("Skill Flags")]
  11.     public bool DoubleJumpEnabled = true;
  12.     public bool WallJumpEnabled = true;
  13.  
  14.     public bool grounded = false;
  15.     public bool touchingWall = false;
  16.     public bool touchingWallRaw = false;
  17.     public bool DoubleJumped = false;
  18.     public bool Dashing = false;
  19.     public bool WallClimbed = false;
  20.  
  21.     [Header("Forces")]
  22.     public float HorizontalSpeed = 10F;
  23.     public float JumpForce = 700F;
  24.     public float WallJumpPushForce = 10F;
  25.     public float WallSlideDownSpeed = -3.5f;
  26.     public float DoubleJumpModifier = 1F;
  27.     public float DashSpeed = 0f;
  28.     public int WallClimbFramesLength = 0;
  29.     public float WallClimbSpeed = 6f;
  30.  
  31.     private float placmentCheckRadius = 0.2f;
  32.     [Header("Placement Checks")]
  33.     public LayerMask GroundLayerMask;
  34.     public Transform GroundCheck;
  35.     public Transform WallCheck;
  36.  
  37.     [Header("Input Smoothing")]
  38.     public int LateJumpAllowance = 3;
  39.     public int FrameIgnoreHAxis = 3;
  40.     public int JumpInputGraceFrames = 3;
  41.     public int WallJumpGraceFrames = 3;
  42.     /// <summary>
  43.     /// The amount of frames to allow a wall jump to ignore the input h-axis
  44.     /// </summary>
  45.     private int wallJumpFrameOverride = 0;
  46.     /// <summary>
  47.     /// The amount of frames to allow plaers to change direction while wall hanging
  48.     /// </summary>
  49.     public int wallGrabFrameOverride = 0;
  50.     private int jumpFutureCheck = 0;
  51.     private bool jumpForseen = false;
  52.     private int wallClimbFrames = 0;
  53.     private int framesSinceLastGrounded = 0;
  54.  
  55.     private Transform dashTarget = null;
  56.  
  57.     //HIDE FROM UNITY EDITOR
  58.     public bool InputEnabled = false;
  59.  
  60.     // Use this for initialization
  61.     void Awake () {
  62.         rigidBody2d = GetComponent<Rigidbody2D> ();
  63.         animator = GetComponent<Animator> ();
  64.     }
  65.    
  66.     // Update is called once per frame
  67.     void Update () {
  68.  
  69.         //TODO Grace period for jumping after walking of an edge, for tight controls
  70.  
  71.         //jump and double jump logic
  72.         if ((grounded || canDoubleJump () || framesSinceLastGrounded < LateJumpAllowance) && !touchingWall && (isJumpInput() || jumpForseen)) {
  73.             animator.SetBool("Ground", false);
  74.             Jump ();
  75.             //dirty hack to avoid force from wall jump and grounded jump to be added together
  76.             grounded = false;
  77.             if(!grounded && framesSinceLastGrounded > LateJumpAllowance)
  78.                 DoubleJumped = true;
  79.         } else if (jumpFutureCheck == 0 && !touchingWall && !grounded && isJumpInput()) {
  80.             jumpFutureCheck = JumpInputGraceFrames;
  81.         }
  82.  
  83.         //wall jumping/sliding should be optional
  84.         if (canWallJump() && isJumpInput())
  85.         {
  86.             rigidBody2d.velocity = new Vector2 (0, 0);
  87.             //Push away from where we are facing on the X axis
  88.             rigidBody2d.AddForce (new Vector2 (facingRight ? WallJumpPushForce * -1 : WallJumpPushForce, JumpForce));
  89.             wallJumpFrameOverride = FrameIgnoreHAxis;
  90.         }
  91.  
  92.         if (Input.GetButtonDown ("Fire1") && InputEnabled) {
  93.             animator.SetTrigger("Attack");
  94.         }
  95.  
  96.         if (Input.GetButtonDown ("Fire2") && InputEnabled) {
  97.             Dash();
  98.         }
  99.  
  100.         //Check for wall jump direction change grace frames
  101.         float h = Input.GetAxis ("Horizontal");
  102.         if (touchingWall && wallGrabFrameOverride == 0) {
  103.             if((h >= 0 && !facingRight) || h <= 0 && facingRight) {
  104.                 wallGrabFrameOverride = WallJumpGraceFrames;
  105.             }
  106.  
  107.         }
  108.     }
  109.  
  110.     void FixedUpdate() {
  111.         framesSinceLastGrounded++;
  112.         bool wasGrounded = grounded;
  113.         bool wasTouchingWallRaw = touchingWallRaw;
  114.         //grounding checks
  115.         grounded = Physics2D.OverlapCircle (GroundCheck.position, placmentCheckRadius, GroundLayerMask);
  116.         //we can't be grounded
  117.         touchingWallRaw = Physics2D.OverlapCircle(WallCheck.position, placmentCheckRadius, GroundLayerMask);
  118.         touchingWall = !grounded && touchingWallRaw;
  119.         float h = Input.GetAxis ("Horizontal");
  120.         //Reset wall climbs after player touches the ground.
  121.         if (!wasGrounded && grounded) {
  122.             WallClimbed = false;
  123.         }
  124.         if (wasGrounded && !grounded)
  125.             framesSinceLastGrounded = 0;
  126.         if (grounded)
  127.             framesSinceLastGrounded = LateJumpAllowance + 1;;
  128.         //stop wall climb when player stops contact with wall;
  129.         if (wasTouchingWallRaw && !touchingWallRaw) {
  130.             wallClimbFrames = 0;
  131.         }
  132.  
  133.         //Activate wallclimb when player is facing a wall and has not wall climbed and is pushing into wall
  134.         if (touchingWallRaw && !WallClimbed && Mathf.Abs (h) > 0 && (isJumpInput() || rigidBody2d.velocity.y > 0) && InputEnabled) {
  135.             wallClimbFrames = WallClimbFramesLength;
  136.         }
  137.  
  138.         if (grounded)
  139.             DoubleJumped = false;
  140.         if (touchingWall)
  141.             grounded = DoubleJumped = false;
  142.  
  143.         if (wallJumpFrameOverride == 0 && wallGrabFrameOverride == 0 && InputEnabled) {
  144.             rigidBody2d.velocity = new Vector2 (h * HorizontalSpeed, rigidBody2d.velocity.y);
  145.         } else {
  146.             if(wallJumpFrameOverride > 0)
  147.                 wallJumpFrameOverride--;
  148.             if(wallGrabFrameOverride > 0)
  149.                 wallGrabFrameOverride--;
  150.         }
  151.  
  152.         if (jumpFutureCheck > 0) {
  153.             jumpFutureCheck--;
  154.             jumpForseen = true;
  155.         } else {
  156.             jumpForseen = false;
  157.         }
  158.  
  159.         if (dashTarget && InputEnabled) {
  160.             rigidBody2d.MovePosition(Vector3.MoveTowards (transform.position, dashTarget.position, DashSpeed * Time.deltaTime));
  161.             if(Vector2.Distance(transform.position, dashTarget.position) < 0.5f)
  162.                 dashTarget = null;
  163.         } else if (Mathf.Abs (h) > 0 && touchingWall && wallJumpFrameOverride == 0 && InputEnabled) {
  164.             if(!isWallRunning())
  165.                 rigidBody2d.velocity = new Vector2 (rigidBody2d.velocity.x, WallSlideDownSpeed);
  166.             else {
  167.                 WallClimbed = true;
  168.                 rigidBody2d.velocity = new Vector2 (rigidBody2d.velocity.x, WallClimbSpeed);
  169.                 wallClimbFrames--;
  170.             }
  171.         }
  172.  
  173.         if (h > 0 && !facingRight && wallGrabFrameOverride == 0)
  174.             Flip ();
  175.         else if (h < 0 && facingRight && wallGrabFrameOverride == 0)
  176.             Flip ();
  177.  
  178.         animator.SetBool ("Ground", grounded);
  179.         animator.SetFloat ("vSpeed", rigidBody2d.velocity.y);
  180.         animator.SetBool ("Wall", touchingWall);
  181.         animator.SetFloat ("Speed", Mathf.Abs(h));
  182.     }
  183.  
  184.     private bool isJumpInput() {
  185.         return InputEnabled && Input.GetButtonDown ("Jump");
  186.     }
  187.  
  188.     private bool canDoubleJump() {
  189.         return DoubleJumpEnabled && !DoubleJumped;
  190.     }
  191.  
  192.     private bool canWallJump() {
  193.         return WallJumpEnabled && touchingWall;
  194.     }
  195.  
  196.     public bool isWallRunning() {
  197.         return wallClimbFrames > 0 && Mathf.Abs(Input.GetAxis ("Horizontal")) > 0 && touchingWallRaw;
  198.     }
  199.  
  200.     void Dash() {
  201.         Transform target = null;
  202.         Collider2D[] hits = Physics2D.OverlapCircleAll (transform.position, 5f);
  203.         foreach (var hit in hits) {
  204.             if(hit.GetComponent<DashTarget>())
  205.                 target = hit.transform;
  206.  
  207.         }
  208.         if (target)
  209.             dashTarget = target;
  210.     }
  211.  
  212.     void Jump() {
  213.         //reset the y velocity for double jumps
  214.         rigidBody2d.velocity = new Vector2 (rigidBody2d.velocity.x, 0);
  215.         float jumpForce = JumpForce;
  216.         if (!grounded)
  217.             jumpForce *= DoubleJumpModifier;
  218.  
  219.  
  220.         rigidBody2d.AddForce(new Vector2(0, jumpForce));
  221.  
  222.         gameObject.BroadcastMessage ("PlayerJumped");
  223.     }
  224.  
  225.     void Flip() {
  226.         facingRight = !facingRight;
  227.         Vector3 theScale = transform.localScale;
  228.         theScale.x *= -1;
  229.         transform.localScale = theScale;
  230.     }
  231.  
  232.     void OnDrawGizmosSelected() {
  233.         Gizmos.color = Color.yellow;
  234.         Gizmos.DrawWireSphere(transform.position, 5f);
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement