Advertisement
Guest User

Untitled

a guest
Jul 9th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using UnityEngine;
  2. public class TestControls1 : MonoBehaviour{
  3.  
  4.     #region Attributes                                                                                                              //Section dedicated to variables declarations and values attributions.
  5.     Animation anim;                                                                                                                 //Defines "anim" as an "Animation" type variable. It will permit to manage the content of the "Animation" component and set the related ingame actions through scripts.
  6.     CharacterController controller;                                                                                                 //Defines "controller" as a "CharacterController" type variable. It will permit to manage the content of the "CharacterController" component and set the related ingame actions through scripts.
  7.     float walkSpeed = 1.6f, runSpeed = 3.2f, kiBoostSpeed = 6.4f, flySpeed = 3.2f, jumpSpeed = 6, Gravity = 0, GroundDistance = 0;  //Defines variables as a "float" type variable (decimals). Here, there is Movements, Gravity and GroundDistance variables.
  8.     bool IsGrounded = false, IsFlying = false;                                                                                      //Defines variable as "boolean" type variables. This type of variable contains only 2 values, true and false. Here, these variables will confirm a defined status (if the character is standing on ground, is in flying state).
  9.     RaycastHit checkGroundHit;                                                                                                      //Defines "checkGroundHit" as a "RayCastHit" type variable. This type of variable will store the collision of the raycast with another collider.
  10.     Vector3 capsMinHeight, capsMaxHeight;                                                                                           //Defines variables as "Vector3" type variable. This type of variable will store the position (world coordinates) of the gameobject under the form of 3 values: x, y and z.
  11.  
  12.     #endregion
  13.  
  14.     void Start (){                                                                                                                  //Execute it's content once at the beginning of the instance.
  15.         anim = GetComponent<Animation> ();                                                                                          //Make "anim" get the "Animation" component's values attached to the same object this script is to.
  16.         controller = GetComponent<CharacterController> ();                                                                          //Make "controller" get the "CharacterController" component's values attached to the same object this script is to.
  17.         capsMinHeight = transform.position + controller.center + Vector3.up * -controller.height * 0.5f;                            //Defines variable more thoroughly.
  18.         capsMaxHeight = capsMinHeight + Vector3.up * controller.height;}                                                            //Defines variable more thoroughly.
  19.  
  20.     public void Update(){                                                                                                           //Execute it's content every each frame.
  21.         CheckMove();                                                                                                                //Calls the CheckMove function.
  22.         checkIsGrounded();                                                                                                          //Calls the checkIsGrounded function.
  23.         ApplyGravity();}                                                                                                            //Calls the ApplyGravity function.
  24.  
  25.     public void checkIsGrounded(){
  26.         if(Physics.CapsuleCast (capsMinHeight, capsMaxHeight, controller.radius, -transform.up, out checkGroundHit, 5)){
  27.             GroundDistance = checkGroundHit.distance;}
  28.         if(checkGroundHit.distance <1){IsGrounded = true; Debug.Log("IsGrounded = " + IsGrounded);}
  29.             else{IsGrounded = false; Debug.Log ("IsGrounded = " +IsGrounded);}}
  30.  
  31.     public void CheckMove(){
  32.         float v = Input.GetAxis("Vertical"), h = Input.GetAxis("Horizontal"), u = Input.GetAxis("Jump");                            //Variables referring values of the inputs stored in Edit/Project Settings/Inputs (menu accessible from the unity editor).                             
  33.  
  34.         //Play adapted animations depending on axises value. "CrossFade" assures a smooth transition between the animations.
  35.         if(v == 0 & h == 0 & u == 0){anim.CrossFade ("idle");}                                                                      //Idle
  36.         if(v > 0.1 | v < - 0.1 | h > 0.1 | h < - 0.1){anim.CrossFade ("walk");}                                                     //Walk
  37.         if(v > 0.4 | v < - 0.4 | h > 0.4 | h < - 0.4){anim.CrossFade ("run");}                                                      //run
  38.  
  39.         //Movements
  40.         if(v > 0.1){controller.Move(transform.forward * walkSpeed * Time.deltaTime);}                                               //Walk forward
  41.         if(h < -0.1){controller.Move(-transform.right * walkSpeed * Time.deltaTime);}                                               //Walk left
  42.         if(v < -0.1){controller.Move(-transform.forward * walkSpeed * Time.deltaTime);}                                             //Walk backward
  43.         if(h > 0.1){controller.Move(transform.right * walkSpeed * Time.deltaTime);}                                                 //Walk right
  44.         if(v > 0.4){controller.Move(transform.forward * runSpeed * Time.deltaTime);}                                                //Run forward
  45.         if(h < -0.4){controller.Move(-transform.right * runSpeed * Time.deltaTime);}                                                //Run left
  46.         if(v < -0.4){controller.Move(-transform.forward * runSpeed * Time.deltaTime);}                                              //Run backward
  47.         if(h > 0.4){controller.Move(transform.right * runSpeed * Time.deltaTime);}                                                  //Run right
  48.         if (u > 0.1) {controller.Move(transform.up * jumpSpeed);}                                                                   //Jump
  49.  
  50.         //Fly Down (for tests)
  51.         if(u < -0.1){controller.Move(-transform.up * flySpeed);}
  52.     }
  53.     public void ApplyGravity(){
  54.         if(!IsGrounded){controller.Move(-transform.up * Gravity * Time.deltaTime);}                                                     //If the player isn't grounded, apply a downward movement using "Gravity" variable as speed and Time.deltatime to scale down the speed to a moderate level
  55.         if(!IsGrounded && Gravity < 30){ Gravity += 0.00001f;}                                                                  //To simulate a bit more realistic Gravity system, the speed must increase, since it's an object with a mass. To make it quite comfortable for this kind of gameplay, a limit will be defined. Until Gravity is equal to 30, increment it by 0.00001
  56.         if(IsGrounded){Gravity = 0;}                                                                                                //If the player is grounded, snap the Gravity action and reset the Gravity variable
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement