Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class TestControls1 : MonoBehaviour{
- #region Attributes //Section dedicated to variables declarations and values attributions.
- 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.
- 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.
- 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.
- 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).
- RaycastHit checkGroundHit; //Defines "checkGroundHit" as a "RayCastHit" type variable. This type of variable will store the collision of the raycast with another collider.
- 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.
- #endregion
- void Start (){ //Execute it's content once at the beginning of the instance.
- anim = GetComponent<Animation> (); //Make "anim" get the "Animation" component's values attached to the same object this script is to.
- controller = GetComponent<CharacterController> (); //Make "controller" get the "CharacterController" component's values attached to the same object this script is to.
- capsMinHeight = transform.position + controller.center + Vector3.up * -controller.height * 0.5f; //Defines variable more thoroughly.
- capsMaxHeight = capsMinHeight + Vector3.up * controller.height;} //Defines variable more thoroughly.
- public void Update(){ //Execute it's content every each frame.
- CheckMove(); //Calls the CheckMove function.
- checkIsGrounded(); //Calls the checkIsGrounded function.
- ApplyGravity();} //Calls the ApplyGravity function.
- public void checkIsGrounded(){
- if(Physics.CapsuleCast (capsMinHeight, capsMaxHeight, controller.radius, -transform.up, out checkGroundHit, 5)){
- GroundDistance = checkGroundHit.distance;}
- if(checkGroundHit.distance <1){IsGrounded = true; Debug.Log("IsGrounded = " + IsGrounded);}
- else{IsGrounded = false; Debug.Log ("IsGrounded = " +IsGrounded);}}
- public void CheckMove(){
- 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).
- //Play adapted animations depending on axises value. "CrossFade" assures a smooth transition between the animations.
- if(v == 0 & h == 0 & u == 0){anim.CrossFade ("idle");} //Idle
- if(v > 0.1 | v < - 0.1 | h > 0.1 | h < - 0.1){anim.CrossFade ("walk");} //Walk
- if(v > 0.4 | v < - 0.4 | h > 0.4 | h < - 0.4){anim.CrossFade ("run");} //run
- //Movements
- if(v > 0.1){controller.Move(transform.forward * walkSpeed * Time.deltaTime);} //Walk forward
- if(h < -0.1){controller.Move(-transform.right * walkSpeed * Time.deltaTime);} //Walk left
- if(v < -0.1){controller.Move(-transform.forward * walkSpeed * Time.deltaTime);} //Walk backward
- if(h > 0.1){controller.Move(transform.right * walkSpeed * Time.deltaTime);} //Walk right
- if(v > 0.4){controller.Move(transform.forward * runSpeed * Time.deltaTime);} //Run forward
- if(h < -0.4){controller.Move(-transform.right * runSpeed * Time.deltaTime);} //Run left
- if(v < -0.4){controller.Move(-transform.forward * runSpeed * Time.deltaTime);} //Run backward
- if(h > 0.4){controller.Move(transform.right * runSpeed * Time.deltaTime);} //Run right
- if (u > 0.1) {controller.Move(transform.up * jumpSpeed);} //Jump
- //Fly Down (for tests)
- if(u < -0.1){controller.Move(-transform.up * flySpeed);}
- }
- public void ApplyGravity(){
- 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
- 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
- if(IsGrounded){Gravity = 0;} //If the player is grounded, snap the Gravity action and reset the Gravity variable
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement