Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class CharController2D : MonoBehaviour {
- private Rigidbody2D rigidBody2d;
- private Animator animator;
- public bool facingRight = true;
- [Header("Skill Flags")]
- public bool DoubleJumpEnabled = true;
- public bool WallJumpEnabled = true;
- public bool grounded = false;
- public bool touchingWall = false;
- public bool touchingWallRaw = false;
- public bool DoubleJumped = false;
- public bool Dashing = false;
- public bool WallClimbed = false;
- [Header("Forces")]
- public float HorizontalSpeed = 10F;
- public float JumpForce = 700F;
- public float WallJumpPushForce = 10F;
- public float WallSlideDownSpeed = -3.5f;
- public float DoubleJumpModifier = 1F;
- public float DashSpeed = 0f;
- public int WallClimbFramesLength = 0;
- public float WallClimbSpeed = 6f;
- private float placmentCheckRadius = 0.2f;
- [Header("Placement Checks")]
- public LayerMask GroundLayerMask;
- public Transform GroundCheck;
- public Transform WallCheck;
- [Header("Input Smoothing")]
- public int LateJumpAllowance = 3;
- public int FrameIgnoreHAxis = 3;
- public int JumpInputGraceFrames = 3;
- public int WallJumpGraceFrames = 3;
- /// <summary>
- /// The amount of frames to allow a wall jump to ignore the input h-axis
- /// </summary>
- private int wallJumpFrameOverride = 0;
- /// <summary>
- /// The amount of frames to allow plaers to change direction while wall hanging
- /// </summary>
- public int wallGrabFrameOverride = 0;
- private int jumpFutureCheck = 0;
- private bool jumpForseen = false;
- private int wallClimbFrames = 0;
- private int framesSinceLastGrounded = 0;
- private Transform dashTarget = null;
- //HIDE FROM UNITY EDITOR
- public bool InputEnabled = false;
- // Use this for initialization
- void Awake () {
- rigidBody2d = GetComponent<Rigidbody2D> ();
- animator = GetComponent<Animator> ();
- }
- // Update is called once per frame
- void Update () {
- //TODO Grace period for jumping after walking of an edge, for tight controls
- //jump and double jump logic
- if ((grounded || canDoubleJump () || framesSinceLastGrounded < LateJumpAllowance) && !touchingWall && (isJumpInput() || jumpForseen)) {
- animator.SetBool("Ground", false);
- Jump ();
- //dirty hack to avoid force from wall jump and grounded jump to be added together
- grounded = false;
- if(!grounded && framesSinceLastGrounded > LateJumpAllowance)
- DoubleJumped = true;
- } else if (jumpFutureCheck == 0 && !touchingWall && !grounded && isJumpInput()) {
- jumpFutureCheck = JumpInputGraceFrames;
- }
- //wall jumping/sliding should be optional
- if (canWallJump() && isJumpInput())
- {
- rigidBody2d.velocity = new Vector2 (0, 0);
- //Push away from where we are facing on the X axis
- rigidBody2d.AddForce (new Vector2 (facingRight ? WallJumpPushForce * -1 : WallJumpPushForce, JumpForce));
- wallJumpFrameOverride = FrameIgnoreHAxis;
- }
- if (Input.GetButtonDown ("Fire1") && InputEnabled) {
- animator.SetTrigger("Attack");
- }
- if (Input.GetButtonDown ("Fire2") && InputEnabled) {
- Dash();
- }
- //Check for wall jump direction change grace frames
- float h = Input.GetAxis ("Horizontal");
- if (touchingWall && wallGrabFrameOverride == 0) {
- if((h >= 0 && !facingRight) || h <= 0 && facingRight) {
- wallGrabFrameOverride = WallJumpGraceFrames;
- }
- }
- }
- void FixedUpdate() {
- framesSinceLastGrounded++;
- bool wasGrounded = grounded;
- bool wasTouchingWallRaw = touchingWallRaw;
- //grounding checks
- grounded = Physics2D.OverlapCircle (GroundCheck.position, placmentCheckRadius, GroundLayerMask);
- //we can't be grounded
- touchingWallRaw = Physics2D.OverlapCircle(WallCheck.position, placmentCheckRadius, GroundLayerMask);
- touchingWall = !grounded && touchingWallRaw;
- float h = Input.GetAxis ("Horizontal");
- //Reset wall climbs after player touches the ground.
- if (!wasGrounded && grounded) {
- WallClimbed = false;
- }
- if (wasGrounded && !grounded)
- framesSinceLastGrounded = 0;
- if (grounded)
- framesSinceLastGrounded = LateJumpAllowance + 1;;
- //stop wall climb when player stops contact with wall;
- if (wasTouchingWallRaw && !touchingWallRaw) {
- wallClimbFrames = 0;
- }
- //Activate wallclimb when player is facing a wall and has not wall climbed and is pushing into wall
- if (touchingWallRaw && !WallClimbed && Mathf.Abs (h) > 0 && (isJumpInput() || rigidBody2d.velocity.y > 0) && InputEnabled) {
- wallClimbFrames = WallClimbFramesLength;
- }
- if (grounded)
- DoubleJumped = false;
- if (touchingWall)
- grounded = DoubleJumped = false;
- if (wallJumpFrameOverride == 0 && wallGrabFrameOverride == 0 && InputEnabled) {
- rigidBody2d.velocity = new Vector2 (h * HorizontalSpeed, rigidBody2d.velocity.y);
- } else {
- if(wallJumpFrameOverride > 0)
- wallJumpFrameOverride--;
- if(wallGrabFrameOverride > 0)
- wallGrabFrameOverride--;
- }
- if (jumpFutureCheck > 0) {
- jumpFutureCheck--;
- jumpForseen = true;
- } else {
- jumpForseen = false;
- }
- if (dashTarget && InputEnabled) {
- rigidBody2d.MovePosition(Vector3.MoveTowards (transform.position, dashTarget.position, DashSpeed * Time.deltaTime));
- if(Vector2.Distance(transform.position, dashTarget.position) < 0.5f)
- dashTarget = null;
- } else if (Mathf.Abs (h) > 0 && touchingWall && wallJumpFrameOverride == 0 && InputEnabled) {
- if(!isWallRunning())
- rigidBody2d.velocity = new Vector2 (rigidBody2d.velocity.x, WallSlideDownSpeed);
- else {
- WallClimbed = true;
- rigidBody2d.velocity = new Vector2 (rigidBody2d.velocity.x, WallClimbSpeed);
- wallClimbFrames--;
- }
- }
- if (h > 0 && !facingRight && wallGrabFrameOverride == 0)
- Flip ();
- else if (h < 0 && facingRight && wallGrabFrameOverride == 0)
- Flip ();
- animator.SetBool ("Ground", grounded);
- animator.SetFloat ("vSpeed", rigidBody2d.velocity.y);
- animator.SetBool ("Wall", touchingWall);
- animator.SetFloat ("Speed", Mathf.Abs(h));
- }
- private bool isJumpInput() {
- return InputEnabled && Input.GetButtonDown ("Jump");
- }
- private bool canDoubleJump() {
- return DoubleJumpEnabled && !DoubleJumped;
- }
- private bool canWallJump() {
- return WallJumpEnabled && touchingWall;
- }
- public bool isWallRunning() {
- return wallClimbFrames > 0 && Mathf.Abs(Input.GetAxis ("Horizontal")) > 0 && touchingWallRaw;
- }
- void Dash() {
- Transform target = null;
- Collider2D[] hits = Physics2D.OverlapCircleAll (transform.position, 5f);
- foreach (var hit in hits) {
- if(hit.GetComponent<DashTarget>())
- target = hit.transform;
- }
- if (target)
- dashTarget = target;
- }
- void Jump() {
- //reset the y velocity for double jumps
- rigidBody2d.velocity = new Vector2 (rigidBody2d.velocity.x, 0);
- float jumpForce = JumpForce;
- if (!grounded)
- jumpForce *= DoubleJumpModifier;
- rigidBody2d.AddForce(new Vector2(0, jumpForce));
- gameObject.BroadcastMessage ("PlayerJumped");
- }
- void Flip() {
- facingRight = !facingRight;
- Vector3 theScale = transform.localScale;
- theScale.x *= -1;
- transform.localScale = theScale;
- }
- void OnDrawGizmosSelected() {
- Gizmos.color = Color.yellow;
- Gizmos.DrawWireSphere(transform.position, 5f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement