Guest User

PlayerLadderClimb

a guest
Jun 20th, 2014
671
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 PlayerLadderClimb: MonoBehaviour {
  5.  
  6.     private PlayerMove playerMove;
  7.     private CharacterMotor characterMotor;
  8.     private Rigidbody playerRB;
  9.     public float ClimbingSpeed=10;
  10.    
  11.     private bool ClimbingLadder;
  12.     private bool CanClimbLadder;
  13.    
  14.     void Start () {
  15.         //Let's initialize our stuff, and store some info.
  16.         playerMove = GetComponent<PlayerMove>();
  17.         characterMotor = GetComponent<CharacterMotor>();
  18.         playerRB = GetComponent<Rigidbody>();
  19.         ClimbingLadder=false;
  20.         CanClimbLadder=true;
  21.     }
  22.        
  23.     // Update is called once per frame
  24.     void Update () {
  25.         //If we're not currently climbing, we won't do anything, but if we are...
  26.         if(ClimbingLadder) {
  27.             //I'll get the vertical axis (Front and back
  28.             float v = Input.GetAxisRaw ("Vertical");
  29.             //I'll create a new vector 3 with the Y being the current vertical axis * climbing speed.
  30.             //So if I'm not inputing anything, the Y will be 0 (Climbing speed times 0 is 0).
  31.             //If i'm inputing positive, it'll be a positive Y, if negative, a negative Y.
  32.             Vector3 speed = new Vector3(0,v*ClimbingSpeed,0);
  33.             //With this new vector3, we'll do a rigid body move position,  times deltatime (current time)
  34.             rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
  35.            
  36.             //If we press the jump button we probably don't want to be in the ladder anymore so let's call our stop climbing function
  37.             if (Input.GetButtonDown ("Jump")) {
  38.                 StopClimbingLadder();
  39.             }
  40.         }
  41.     }
  42.    
  43.     void OnTriggerEnter(Collider other) {
  44.         //If I can't climb (just stopped climbing) let's not do anything!
  45.         if(!CanClimbLadder){
  46.             return;
  47.         }
  48.         //If the object has the tag Ladder and I can Climb...
  49.         if(other.gameObject.tag=="Ladder"&&CanClimbLadder) {
  50.             //Set my Climbing Ladder to true
  51.             ClimbingLadder=true;
  52.             //Disable my Player Move, let me handle the input.
  53.             playerMove.enabled=false;
  54.             //Set the RigidBody to Kinematic, I GOT THIS BRO!
  55.             playerRB.isKinematic=true;
  56.             //Set the Bool Climbing on the animnator to true
  57.             //So Set up your climbing animation to use that bool.
  58.             playerMove.animator.SetBool ("Climbing", true);
  59.         }
  60.     }
  61.    
  62.     void OnTriggerExit(Collider other) {
  63.         //If a object with the gametag ladder exited my Trigger...
  64.         if(other.gameObject.tag=="Ladder") {
  65.             //Stop climbing! let's get control back!
  66.             StopClimbingLadder();
  67.         }
  68.     }
  69.    
  70.     void StopClimbingLadder() {
  71.         //Let's set the can climb, climbing to false So we're not climbing and we can't climb
  72.         CanClimbLadder=false;
  73.         ClimbingLadder=false;
  74.         //Let's turn on our player movement and uncheck rigidbody kinematic
  75.         //So that gravity will work again on us and we'll be able to control ourselves.
  76.         playerMove.enabled=true;
  77.         playerRB.isKinematic=false;
  78.         //Let's turn off our climbing animation
  79.         playerMove.animator.SetBool ("Climbing", false);
  80.         //And in a couple of F time we'll want to climb agian so we'll start this coroutine
  81.         StartCoroutine(CanClimbAgain());
  82.     }
  83.    
  84.     IEnumerator CanClimbAgain()
  85.     {
  86.         //Let's wait a delay amount of time to start.
  87.         yield return StartCoroutine(Wait(0.5f));
  88.         //Now we can climb again! :D
  89.         CanClimbLadder=true;
  90.     }
  91.    
  92.        
  93.     //This is a fancy wait coroutine for the others to use.
  94.     IEnumerator Wait(float duration)
  95.     {
  96.         for (float timer = 0; timer < duration; timer += Time.deltaTime)
  97.             yield return 0;
  98.     }
  99. }
RAW Paste Data