Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class TestControls1 : MonoBehaviour{
- Animation anim;
- CharacterController controller;
- float walkSpeed = 1.6f, runSpeed = 3.2f, kiBoostSpeed = 6.4f, flySpeed = 3.2f, jumpSpeed = 6f, Gravity = 0f; //Speed and Gravity variables
- bool IsGrounded = false, IsFlying = false; //Are we grounded ? Flying ?
- void Start (){
- anim = GetComponent<Animation>(); //Make "anim" get the Animation component attached to the same object this script is to
- controller = GetComponent<CharacterController>();} //Make "controller" get the CharacterController component attached to the same object this script is to
- public void Update(){ //Execute it's content every each frame
- CheckMove(); //Calls the CheckMove function
- ApplyGravity(); //Calls the ApplyGravity function
- }
- public void OnControllerColliderHit(ControllerColliderHit hit){
- //Verifies the collided object's angle compared to the character. 0.707 = sin(45)
- if(hit.normal.y > 0.707){IsGrounded = true;
- Debug.Log("IsGrounded =" + IsGrounded);}
- if(hit.normal.y < 0.707){IsGrounded = false;
- Debug.Log("IsGrounded =" + IsGrounded);}}
- public void CheckMove(){
- 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)
- //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 == 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
- if(IsGrounded == false && Gravity < 30f){ Gravity += 0.00001f;} //Until Gravity is equal to 30, increment it by 0.00001
- if(IsGrounded == true){Gravity = 0f;} //If the player is grounded, snap the Gravity action and reset the Gravity variable
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement