Advertisement
Guest User

PlayerWallJump

a guest
Jun 20th, 2014
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerWallJump : MonoBehaviour {
  5.  
  6.     //We'll use those 3 to communicate with the rest of the kit.
  7.     private PlayerMove playerMove;
  8.     private CharacterMotor characterMotor;
  9.    
  10.     public bool CanWallJump;
  11.  
  12.     //We'll pick the grounded bool from the Animator and store here to know if we're grounded.
  13.     private bool GroundedBool;
  14.     private GameObject CurrentWall;
  15.     // Use this for initialization
  16.     void Start () {
  17.         playerMove = GetComponent<PlayerMove>();
  18.         characterMotor = GetComponent<CharacterMotor>();
  19.         CanWallJump=false;
  20.     }
  21.    
  22.     void Update() {
  23.         if(!CanWallJump){
  24.             return;
  25.         }
  26.         if (Input.GetButtonDown ("Jump") && !GroundedBool && CanWallJump) {
  27.  
  28.             Vector3 WallPos = CurrentWall.transform.position;
  29.             Vector3 myPos = transform.position;
  30.             myPos.y=0;
  31.             WallPos.y=0;
  32.             transform.rotation = Quaternion.LookRotation(myPos - WallPos);
  33.            
  34.             Vector3 Direction = gameObject.transform.forward;
  35.            
  36.             Direction.y=0;
  37.             Direction.x=Direction.x*15;
  38.             Direction.z=0;
  39.            
  40.             Debug.Log("JumpTo "+ gameObject.transform.forward+Direction);
  41.             gameObject.rigidbody.AddForce (Direction*50);
  42.             playerMove.Jump (new Vector3 (0,12,0));
  43.             playerMove.animator.Play("Jump1",0);
  44.             //And lets set the double jump to false!
  45.             CanWallJump=false;
  46.         }
  47.         if(CanWallJump&&!GroundedBool) {
  48.             //characterMotor.RotateToDirection(transform.position-CurrentWall.transform.position,900f,true);
  49.             Vector3 WallPos = CurrentWall.transform.position;
  50.             Vector3 myPos = transform.position;
  51.             myPos.y=0;
  52.             WallPos.y=0;
  53.             transform.rotation = Quaternion.LookRotation(WallPos - myPos);
  54.         }
  55.     }
  56.    
  57.     //This is an udpate that is called less frequently
  58.     void FixedUpdate () {
  59.         //Let's pick the Grounded Bool from the animator, since the player grounded bool is private and we can't get it directly..
  60.         GroundedBool = playerMove.animator.GetBool("Grounded");
  61.     }
  62.    
  63.     void OnTriggerEnter(Collider other) {
  64.         if(other.gameObject.tag=="Wall") {
  65.             CanWallJump=true;
  66.             CurrentWall=other.gameObject;
  67.         }
  68.     }
  69.    
  70.     void OnTriggerExit(Collider other) {
  71.         if(other.gameObject.tag=="Wall") {
  72.             CanWallJump=false;
  73.             CurrentWall=null;
  74.         }
  75.     }
  76.    
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement