Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. [RequireComponent(typeof(Controller2D))]
  6. public class Player : MonoBehaviour {
  7.  
  8.     public float maxJumpHeight = 4;
  9.     public float minJumpHeight = 1;
  10.     public float timeToJumpApex = .4f;
  11.     float accelerationTimeAirborne = .2f;
  12.     float accelerationTimeGrounded = .1f;
  13.     static float origMoveSpeed = 6;
  14.     float moveSpeed = origMoveSpeed;
  15.  
  16.     public Vector2 wallJumpClimb;
  17.     public Vector2 wallJumpOff;
  18.     public Vector2 wallLeap;
  19.  
  20.     public float wallSlideSpeedMax = 3;
  21.     public float wallStickTime = .25f;
  22.     float timeToWallUnstick;
  23.  
  24.     float gravity;
  25.     float maxJumpVelocity;
  26.     float minJumpVelocity;
  27.     Vector3 velocity;
  28.     float velocityXSmoothing;
  29.  
  30.     float waitUntil;
  31.     private bool doubleJump;
  32.     /*
  33.         private bool dashSpeed;
  34.         private bool keyPressedTwice;*/
  35.  
  36.     public float buttonCoolerOrig = .8f;
  37.     float buttonCooler;
  38.     int buttonCount = 0;
  39.     [Range (0,5)] //so u dont set it to something dumb like 9001
  40.     public float speedMultiplier = 2.0f;
  41.  
  42.     Controller2D controller;
  43.  
  44.     void Start() {
  45.         waitUntil = Time.time;
  46.         buttonCooler = buttonCoolerOrig;
  47.         controller = GetComponent<Controller2D>();
  48.  
  49.         gravity = -(2 * maxJumpHeight) / Mathf.Pow(timeToJumpApex, 2);
  50.         maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
  51.         minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
  52.         print("Gravity:" + gravity + "Jump Velocity:" + maxJumpVelocity);
  53.     }
  54.  
  55.     void FixedUpdate() {
  56.         //float waitUntil = Time.time;
  57.         //print("moveSpeed is :" + moveSpeed);
  58.         //Debug.Log(moveSpeed);
  59.         if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow)) {
  60.  
  61.             if (buttonCooler > 0 && buttonCount == 1 && waitUntil <= Time.time/*Number of Taps you want Minus One*/) {
  62.                 //Has double tapped
  63.                 //Debug.Log("Double Tapped");
  64.                 moveSpeed *= speedMultiplier;
  65.                 //buttonCooler -= 2 * Time.deltaTime;
  66.                 Debug.Log("DoubleTapped: " + Time.time);
  67.                 waitUntil = Time.time + 0.2f;
  68.                 print(waitUntil);
  69.             } else {
  70.                 buttonCooler = buttonCoolerOrig;
  71.                 buttonCount += 1;            
  72.             }          
  73.         }
  74.         if (buttonCooler > 0) {
  75.             buttonCooler -= 1 * Time.deltaTime;
  76.         } else {
  77.             buttonCount = 0;
  78.             //Debug.Log("Reset");
  79.         }      
  80.         checkMoveSpeed();
  81.     }
  82.  
  83.     private void checkMoveSpeed() {
  84.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));  
  85.         if (input.x == 0) {
  86.             moveSpeed = origMoveSpeed;
  87.             //Debug.Log("Input 0'd");    
  88.         }
  89.         if (moveSpeed > origMoveSpeed * speedMultiplier) {
  90.             moveSpeed = origMoveSpeed * speedMultiplier;
  91.         }
  92.     }
  93.  
  94.     void Update() {
  95.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  96.         int wallDirX = (controller.collisions.left) ? -1 : 1;
  97.  
  98.  
  99.         float targetVelocityX = input.x * moveSpeed;
  100.  
  101.  
  102.  
  103.         velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne);
  104.         //print(velocity.x);
  105.         bool wallSliding = false;
  106.         if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) {
  107.             wallSliding = true;
  108.  
  109.             if (velocity.y < -wallSlideSpeedMax) {
  110.                 velocity.y = -wallSlideSpeedMax;
  111.             }
  112.  
  113.             if (timeToWallUnstick > 0) {
  114.                 velocityXSmoothing = 0;
  115.                 velocity.x = 0;
  116.  
  117.                 if (input.x != wallDirX && input.x != 0) {
  118.                     timeToWallUnstick -= Time.deltaTime;
  119.                 }
  120.                 else {
  121.                     timeToWallUnstick = wallStickTime;
  122.                 }
  123.             }
  124.             else {
  125.                 timeToWallUnstick = wallStickTime;
  126.             }
  127.         }
  128.  
  129.         if (Input.GetKeyDown(KeyCode.Space)) {
  130.             if (wallSliding) {
  131.                 doubleJump = true;
  132.                 if (wallDirX == input.x) {
  133.                     velocity.x = -wallDirX * wallJumpClimb.x;
  134.                     velocity.y = wallJumpClimb.y;
  135.                 }
  136.                 else if (input.x == 0) {
  137.                     velocity.x = -wallDirX * wallJumpOff.x;
  138.                     velocity.y = wallJumpOff.y;
  139.                 }
  140.                 else {
  141.                     velocity.x = -wallDirX * wallLeap.x;
  142.                     velocity.y = wallLeap.y;
  143.                 }
  144.             }
  145.  
  146.             if (controller.collisions.below) {
  147.                 velocity.y = maxJumpVelocity;
  148.             }
  149.         }
  150.         if (Input.GetKeyUp(KeyCode.Space))
  151.             if (velocity.y > minJumpVelocity) {
  152.                 velocity.y = minJumpVelocity;
  153.             }
  154.  
  155.         if (controller.collisions.below || wallSliding) {
  156.             doubleJump = true;
  157.         }
  158.        
  159.         if (!controller.collisions.below && doubleJump == true) {
  160.             if (Input.GetKeyDown(KeyCode.Space)) {
  161.                 velocity.y = maxJumpVelocity;
  162.                 doubleJump = false;
  163.             }      
  164.         }
  165.  
  166.         velocity.y += gravity * Time.deltaTime;
  167.         controller.Move(velocity * Time.deltaTime, input);
  168.  
  169.         if (controller.collisions.above || controller.collisions.below) {
  170.             velocity.y = 0;
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement