Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  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.     private GroundPound groundPound;
  13.  
  14.  
  15.  
  16.     //This is to keep track if we have double jumped already or if we can double jump  
  17.     private bool CanDoubleJump = true;
  18.     //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.
  19.     private float LastY;
  20.     //We'll pick the grounded bool from the Animator and store here to know if we're grounded.
  21.     private bool GroundedBool;
  22.     private bool Pounding;
  23.  
  24.  
  25.     private bool CanPound = false;
  26.    
  27.     // Use this for initialization
  28.     void Start () {
  29.         //Pick the Player Move component
  30.         playerMove = GetComponent<PlayerMove>();
  31.         groundPound = GetComponent<GroundPound> ();
  32.  
  33.     }
  34.    
  35.     // Update is called once per frame
  36.     void Update() {
  37.         //If we receive a jump button down, we're not grounded and we can double jump...
  38.         if (Input.GetButtonDown ("Jump") && !GroundedBool && CanDoubleJump && !Pounding) {
  39.             //Do a jump with the first jump force! :D
  40.             playerMove.Jump (playerMove.jumpForce);
  41.             //And lets set the double jump to false!
  42.             CanDoubleJump=false;
  43.             CanPound=true;
  44.  
  45.  
  46.             if (Input.GetButtonDown ("Jump") && !GroundedBool && !CanDoubleJump && Pounding) {
  47.                     //Do a jump with the first jump force! :D
  48.                 groundPound.Pound (playerMove.poundJumpForce);
  49.                     //And lets set the double jump to false!
  50.                     CanDoubleJump=false;
  51.                     CanPound=true;
  52.  
  53.                 if (Input.GetButtonDown ("Jump") && !GroundedBool && !CanDoubleJump && !Pounding) {
  54.                     //Do a jump with the first jump force! :D
  55.                     groundPound.Pound (playerMove.poundJumpForce);
  56.                     //And lets set the double jump to false!
  57.                     CanDoubleJump=false;
  58.                     CanPound=false;
  59.  
  60.                 }
  61.             }
  62.         }
  63.     }
  64.    
  65.     //This is an udpate that is called less frequently
  66.     void FixedUpdate () {
  67.         //Let's pick the Grounded Bool from the animator, since the player grounded bool is private and we can't get it directly..
  68.         GroundedBool = playerMove.animator.GetBool ("Grounded");
  69.         Pounding = playerMove.animator.GetBool ("Pounding");
  70.  
  71.         //If I can't double jump...
  72.         if (!CanDoubleJump && !Pounding && GroundedBool) {
  73.             //But I'm grounded
  74.  
  75.             //Then I should turn my Double Jump to on because I can certainly double jump again.
  76.             CanDoubleJump = true;
  77.             CanPound = true;
  78.  
  79.             if (!CanDoubleJump && Pounding && !GroundedBool) {
  80.  
  81.                
  82.                 //Then I should turn my Double Jump to on because I can certainly double jump again.
  83.                 CanDoubleJump = false;
  84.                 CanPound = true;
  85.  
  86.                 if (!CanDoubleJump && !Pounding && !GroundedBool) {
  87.  
  88.                    
  89.                    
  90.                     //Then I should turn my Pound to on because I can certainly pound.
  91.                     CanDoubleJump = false;
  92.                     CanPound = false;
  93.        
  94.  
  95.        
  96.         //If shouldn't be able to double jump after reaching the highest jump point, we'll need this part of the code
  97.         if(!CanAfterHighestJumpPoint) {
  98.             //If i'm not grounded
  99.             if(!GroundedBool){
  100.                 //If my current Y position is less than my Previously recorded Y position, then I'm going down
  101.                     //So, I'm going down, I shouldn't be able to double jump anymore.
  102.                     CanDoubleJump=false;
  103.                     CanPound=true;
  104.                
  105.                
  106.                 }
  107.                 //Anyways, lets record the LastY position for a check later.
  108.                 LastY=gameObject.transform.position.y;
  109.                         }
  110.                     }
  111.                 }
  112.            
  113.             }
  114.         }
  115.  
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement