Guest User

OneWayPlatformEnabler

a guest
Jun 23rd, 2014
481
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 OneWayPlatformEnabler : MonoBehaviour {
  5.  
  6.     //We need to know which layer number our regular player has
  7.     public int PlayerLayer=8;
  8.     //Also what layer he should change to to be able to pass through platforms
  9.     public int JumpingUpLayer=10;
  10.     //Lastly, this is just to know if we should use the Double tap to fall down platforms or not.
  11.     public bool CanDoubleTapDown;
  12.  
  13.     //We'll use this to communicate with the playerMove.
  14.     private PlayerMove playerMove;
  15.  
  16.     //We'll use this to know if we should change player layers or just leave it there.
  17.     private bool CanPassThroughPlatforms = false;
  18.     //We need to know if we're falling down or still going up, to choose our layer accordingly
  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.    
  23.     //To detect a double tap, we'll store time since last tap here.
  24.     private float doubleTapTime;
  25.    
  26.     // Use this for initialization
  27.     void Start () {
  28.         //Pick the Player Move component
  29.         playerMove = GetComponent<PlayerMove>();
  30.     }
  31.    
  32.     void Update()
  33.     {
  34.         //The Update would only be used for the double tapping, so if we don't want it, stop here.
  35.         if(!CanDoubleTapDown) {
  36.             return;
  37.         }
  38.         //Otherwise lets create a bool for our DoubleTap and set it to false.
  39.         bool doubleTapS = false;
  40.  
  41.         //If we received a key down S
  42.         if (Input.GetKeyDown(KeyCode.S))
  43.         {
  44.             //Check last time the double tap happened. Was it less than .3f seconds ago?
  45.             if (Time.time < doubleTapTime + .3f)
  46.             {
  47.                 //Yes it was!! turn our bool to true.
  48.                 doubleTapS = true;
  49.             }
  50.             //Wether it was or wasn't, let's store the time of this tap for further comparison.
  51.             doubleTapTime = Time.time;
  52.         }
  53.  
  54.         //If our bool was turned to true...
  55.         if (doubleTapS)
  56.         {
  57.             //We should start a coroutine to change our layer so we can fall, then change it back on.
  58.             StartCoroutine(CollisionBackOn());
  59.         }
  60.     }
  61.    
  62.     IEnumerator CollisionBackOn()
  63.     {
  64.         //Change our layer to the layer that allows our fall
  65.         playerMove.animator.Play("Jump1",0);
  66.         gameObject.layer=JumpingUpLayer;
  67.         //Wait 0.5f for us to fall
  68.         yield return new WaitForSeconds(0.5f);
  69.         //Then turn it back to the layer that lets us collide with that!
  70.         gameObject.layer=PlayerLayer;
  71.     }
  72.    
  73.     //This is an udpate that is called less frequently
  74.     void FixedUpdate () {
  75.         //Let's pick the Grounded Bool from the animator, since the player grounded bool is private and we can't get it directly..
  76.         GroundedBool = playerMove.animator.GetBool("Grounded");
  77.         if(!GroundedBool){
  78.             //If my current Y position is less than my Previously recorded Y position, then I'm going down
  79.             if(gameObject.transform.position.y<LastY) {
  80.                 //So, I'm going down, I should be able to collider with platforms! Change our layer accordingly
  81.                 if(CanPassThroughPlatforms) {
  82.                     CanPassThroughPlatforms=false;
  83.                     gameObject.layer=PlayerLayer;
  84.                 }
  85.             } else {
  86.                 //I'm going up, let's change our layer so I can't collide with platforms.
  87.                 if(!CanPassThroughPlatforms) {
  88.                     CanPassThroughPlatforms=true;
  89.                     gameObject.layer=JumpingUpLayer;
  90.                 }
  91.             }
  92.             //Anyways, lets record the LastY position for a check later.
  93.             LastY=gameObject.transform.position.y;
  94.         }
  95.     }
  96. }
RAW Paste Data