Guest User

DoubleJumpEnabler

a guest
Jun 19th, 2014
968
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DoubleJumpEnabler : MonoBehaviour {
  5.  
  6.     //This will be used to only allow the double jump if it's before highest jump point. Or not, you choose!
  7.     public bool CanAfterHighestJumpPoint=true;
  8.    
  9.     //We'll use this to communicate with the playerMove.
  10.     private PlayerMove playerMove;
  11.  
  12.     //This is to keep track if we have double jumped already or if we can double jump  
  13.     private bool CanDoubleJump = true;
  14.     //In case of us wanting to only double jump if we're still going up on our jump, we need to store the last Y position.
  15.     private float LastY;
  16.     //We'll pick the grounded bool from the Animator and store here to know if we're grounded.
  17.     private bool GroundedBool;
  18.    
  19.     // Use this for initialization
  20.     void Start () {
  21.         //Pick the Player Move component
  22.         playerMove = GetComponent<PlayerMove>();
  23.     }
  24.    
  25.     // Update is called once per frame
  26.     void Update() {
  27.         //If we receive a jump button down, we're not grounded and we can double jump...
  28.         if (Input.GetButtonDown ("Jump") && !GroundedBool && CanDoubleJump) {
  29.             //Do a jump with the first jump force! :D
  30.             playerMove.Jump (playerMove.jumpForce);
  31.             //And lets set the double jump to false!
  32.             CanDoubleJump=false;
  33.         }
  34.     }
  35.    
  36.     //This is an udpate that is called less frequently
  37.     void FixedUpdate () {
  38.         //Let's pick the Grounded Bool from the animator, since the player grounded bool is private and we can't get it directly..
  39.         GroundedBool = playerMove.animator.GetBool("Grounded");
  40.         //If I can't double jump...
  41.         if(!CanDoubleJump) {
  42.             //But I'm grounded
  43.             if(GroundedBool){
  44.                 //Then I should turn my Double Jump to on because I can certainly double jump again.
  45.                 CanDoubleJump=true;
  46.             }
  47.         }
  48.         //If shouldn't be able to double jump after reaching the highest jump point, we'll need this part of the code
  49.         if(!CanAfterHighestJumpPoint) {
  50.             //If i'm not grounded
  51.             if(!GroundedBool){
  52.                 //If my current Y position is less than my Previously recorded Y position, then I'm going down
  53.                 if(gameObject.transform.position.y<LastY) {
  54.                     //So, I'm going down, I shouldn't be able to double jump anymore.
  55.                     CanDoubleJump=false;
  56.                 }
  57.                 //Anyways, lets record the LastY position for a check later.
  58.                 LastY=gameObject.transform.position.y;
  59.             }
  60.         }
  61.     }
  62. }
RAW Paste Data