Advertisement
Guest User

Untitled

a guest
May 15th, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5.  
  6. public class PlayerController : MonoBehaviour {
  7.  
  8.     private Animator animator;
  9.  
  10.  
  11.     public bool inclineTilting = true;
  12.     private float walkingSpeed = 5f;
  13.     private float runningSpeed = 9f;
  14.     private float currentSpeed;
  15.     private float targetSpeed;
  16.     CharacterController characterController;
  17.     public float gravity = -20f;
  18.     private bool running;
  19.     private float turnSmoothTime = 0.1f;
  20.     private float turnSmoothVelocity;
  21.     private float  targetRotation;
  22.     private float turnSmoothSpeed = 15f;
  23.     private float angularSpeed = 4.0f;
  24.  
  25.     private float speedSmoothTime = 0.05f;
  26.  
  27.     private float runningSpeedSmoothTime = 0.05f;
  28.     private float runningSpeedVelocity;
  29.     private float velocityY;
  30.     private float jumpHeight = 10f;
  31.     private bool isFirstJump = true;
  32.     private bool landing = false;
  33.     private Quaternion quatTargetRotation;
  34.  
  35.  
  36.     // Use this for initialization
  37.     void Start () {
  38.         characterController = GetComponent<CharacterController>();
  39.         animator = GetComponent<Animator>();
  40.     }
  41.    
  42.     // Update is called once per frame
  43.     void Update () {
  44.         HandleMovement();
  45.         HandleJump();
  46.     }
  47.  
  48.    
  49.  
  50.     void HandleJump(){
  51.         if(Input.GetButtonDown("Jump")){ // first jump
  52.             if(isFirstJump){
  53.                 animator.SetBool("jump", true);
  54.                 velocityY = Mathf.Sqrt(-2 * gravity * jumpHeight);
  55.                 isFirstJump = !isFirstJump;
  56.             }else if(!isFirstJump && animator.GetBool("jump")){
  57.                 velocityY = Mathf.Sqrt(-2 * gravity * jumpHeight * 1.5f);
  58.                 animator.SetBool("jump", false);
  59.                 animator.SetBool("secondJump", true);
  60.             }
  61.         }
  62.         GroundedCheck();
  63.     }
  64.  
  65.     private void GroundedCheck() {
  66.  
  67.         RaycastHit hit;
  68.         Vector3 ray = transform.TransformDirection(Vector3.down);
  69.  
  70.         if(characterController.isGrounded || Physics.Raycast(transform.position, ray, 2.0f)) {
  71.             animator.SetBool("onAir", false);
  72.         }else {
  73.             animator.SetBool("onAir", true);
  74.         }
  75.        
  76.     }
  77.  
  78.     void HandleMovement(){
  79.        
  80.  
  81.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  82.         Vector2 inputDir = input.normalized;
  83.  
  84.         // transform.eulerAngles = Vector3.up * Camera.main.transform.eulerAngles.y;
  85.        
  86.         if(inputDir != Vector2.zero){
  87.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg +  Camera.main.transform.eulerAngles.y;
  88.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
  89.         }
  90.        
  91.         if(!characterController.isGrounded){
  92.             velocityY += Time.deltaTime * gravity;
  93.         }
  94.  
  95.         // Debug.Log(velocityY);
  96.         running = Input.GetKey(KeyCode.LeftShift);
  97.         if(!animator.GetBool("attacking")){
  98.             if(!landing){
  99.                 targetSpeed = (running ? runningSpeed : walkingSpeed) * inputDir.magnitude;
  100.             }else{
  101.                 targetSpeed = 0.0f;
  102.             }
  103.         }else{
  104.             targetSpeed = 0.0f;
  105.         }
  106.  
  107.         InclineTilt(inputDir);
  108.        
  109.         currentSpeed = Mathf.SmoothDampAngle(currentSpeed, targetSpeed, ref runningSpeedVelocity, runningSpeedSmoothTime);
  110.         Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
  111.         characterController.Move(velocity * Time.deltaTime);
  112.  
  113.         animator.SetFloat("speedPercent", (running ? 1f : 0.5f) * inputDir.magnitude, speedSmoothTime, Time.deltaTime);
  114.     }
  115.  
  116.     private void InclineTilt(Vector2 inputDir) {
  117.         if(inclineTilting && inputDir != Vector2.zero) {
  118.             RaycastHit hit;
  119.             Vector3 ray = transform.TransformDirection(Vector3.down);
  120.             if(Physics.Raycast(transform.position, ray, out hit)) {
  121.                 quatTargetRotation = Quaternion.FromToRotation(Vector3.up, hit.normal) * transform.rotation;
  122.                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.FromToRotation(Vector3.up, hit.normal), 0.5f * Time.deltaTime);
  123.             }
  124.         }
  125.     }
  126.  
  127.  
  128.     public void Landed(){
  129.         animator.SetBool("jump", false);
  130.         animator.SetBool("secondJump", false);
  131.         landing = false;
  132.         isFirstJump = true;
  133.     }
  134.  
  135.     public void MidAirJumpCompleted(){
  136.         animator.SetBool("secondJump", false);
  137.     }
  138.  
  139.     public void LandingStarts(){
  140.         Debug.Log("landing");
  141.         landing = true;
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement