Advertisement
Guest User

Untitled

a guest
Jul 8th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using UnityEngine;
  2. public class TestControls1 : MonoBehaviour{
  3.  
  4.     Animation anim;
  5.     CharacterController controller;
  6.     float walkSpeed = 1.6f, runSpeed = 3.2f, kiBoostSpeed = 6.4f, flySpeed = 3.2f, jumpSpeed = 6f, Gravity = 0f; //Speed and Gravity variables
  7.     bool IsGrounded = false, IsFlying = false; //Are we grounded ? Flying ?
  8.  
  9.     void Start (){
  10.         anim = GetComponent<Animation>(); //Make "anim" get the Animation component attached to the same object this script is to
  11.         controller = GetComponent<CharacterController>();} //Make "controller" get the CharacterController component attached to the same object this script is to
  12.  
  13.     public void Update(){ //Execute it's content every each frame
  14.         CheckMove(); //Calls the CheckMove function
  15.         ApplyGravity(); //Calls the ApplyGravity function
  16.     }
  17.  
  18.     public void OnControllerColliderHit(ControllerColliderHit hit){
  19.         //Verifies the collided object's angle compared to the character. 0.707 = sin(45)
  20.         if(hit.normal.y > 0.707){IsGrounded = true;
  21.             Debug.Log("IsGrounded =" + IsGrounded);}
  22.         if(hit.normal.y < 0.707){IsGrounded = false;
  23.             Debug.Log("IsGrounded =" + IsGrounded);}}
  24.  
  25.     public void CheckMove(){
  26.         float v = Input.GetAxis("Vertical"), h = Input.GetAxis("Horizontal"), u = Input.GetAxis("Jump"); //Variables referencing values of the inputs stored in Edit/Project Settings/Inputs (menu accessible from the unity editor)
  27.  
  28.         //Play adapted animations depending on axises value. "CrossFade" assures a smooth transition between the animations.
  29.         if (v == 0 & h == 0 & u == 0){anim.CrossFade ("idle");} //Idle
  30.         if (v > 0.1 | v < - 0.1 | h > 0.1 | h < - 0.1){anim.CrossFade ("walk");} //Walk
  31.         if (v > 0.4 | v < - 0.4 | h > 0.4 | h < - 0.4){anim.CrossFade ("run");} //run
  32.  
  33.         //Movements
  34.         if(v > 0.1){controller.Move(transform.forward * walkSpeed * Time.deltaTime);}       //Walk forward
  35.         if(h < -0.1){controller.Move(-transform.right * walkSpeed * Time.deltaTime);}       //Walk left
  36.         if(v < -0.1){controller.Move(-transform.forward * walkSpeed * Time.deltaTime);}     //Walk backward
  37.         if(h > 0.1){controller.Move(transform.right * walkSpeed * Time.deltaTime);}         //Walk right
  38.         if(v > 0.4){controller.Move(transform.forward * runSpeed * Time.deltaTime);}        //Run forward
  39.         if(h < -0.4){controller.Move(-transform.right * runSpeed * Time.deltaTime);}        //Run left
  40.         if(v < -0.4){controller.Move(-transform.forward * runSpeed * Time.deltaTime);}      //Run backward
  41.         if(h > 0.4){controller.Move(transform.right * runSpeed * Time.deltaTime);}          //Run right
  42.         if (u > 0.1) {controller.Move(transform.up * jumpSpeed);}                           //Jump
  43.  
  44.         //Fly Down (for tests)
  45.         if(u < -0.1){controller.Move(-transform.up * flySpeed);}
  46.     }
  47.     public void ApplyGravity(){
  48.         if(IsGrounded == false){controller.Move(-transform.up * Gravity * Time.deltaTime);} //If the player isn't grounded, apply a downward movement using "Gravity" variable as speed and Time.delatatime to scale down the speed to a moderate level
  49.         if(IsGrounded == false && Gravity < 30f){ Gravity += 0.00001f;} //Until Gravity is equal to 30, increment it by 0.00001
  50.         if(IsGrounded == true){Gravity = 0f;} //If the player is grounded, snap the Gravity action and reset the Gravity variable
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement