Advertisement
Guest User

PlayerController

a guest
Mar 7th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.12 KB | None | 0 0
  1. private bool running, jumping, crouching;
  2.     private float currentSpeed, velocityY, animationSpeedPercent;
  3.     Animator animator;
  4.     ClimbingSystem climbingSystem;
  5.     CharacterController controller;
  6.  
  7.     [Header("Debug Only")]
  8.     [SerializeField] private float targetSpeed;
  9.     [SerializeField] private float acceleration;
  10.  
  11.     [Header("Input Strings")]
  12.     [SerializeField] private string sprintButton;
  13.     [SerializeField] private string jumpButton;
  14.     [SerializeField] private string crouchButton;
  15.  
  16.     [Header("Character Parameters")]
  17.     [SerializeField] private float startWalkSpeed;
  18.     private float walkSpeed;
  19.     [SerializeField] private float maxWalkSpeed;
  20.     [SerializeField] private float startAcceleration;
  21.     [SerializeField] private float maxAcceleration;
  22.     [SerializeField] private float runSpeed;
  23.     [SerializeField] private float crouchSpeed;
  24.     [SerializeField] private float jumpHeight = 1;
  25.     [SerializeField] private float crouchCharacterHeight = 1f;
  26.     [SerializeField] private float normalCharacterHeight = 1.6f;
  27.     [SerializeField] private float jumpCharacterHeight = 1f;
  28.     [SerializeField] [Range(0, 1f)] private float heightSmoothTime = 0.5f;
  29.     [SerializeField] [Range(0, 1f)] private float turnSmoothTime = 0.2f;
  30.     private float turnSmoothVelocity;
  31.     [SerializeField] [Range(0, 1f)] private float speedSmoothTime = 0.1f;
  32.     private float speedSmoothVelocity;
  33.     [SerializeField] private float gravity = -12.0f;
  34.     [SerializeField] [Range(0, 1f)] private float airControlPercent;
  35.  
  36.     [Header("Climbing System Parameters")]
  37.     [SerializeField] private bool canClimb;
  38.     [SerializeField] private bool canBrace;
  39.     private float yOffset;
  40.     [SerializeField] float bracedYOffset = 1.2f;
  41.     [SerializeField] float hangingYOffset = 1.8f;
  42.  
  43.     [Header("Other")]
  44.     [SerializeField] private GameObject cameraT;
  45.  
  46.     void Start ()
  47.     {
  48.         animator = GetComponent<Animator>();
  49.         controller = GetComponent<CharacterController>();
  50.         climbingSystem = GetComponent<ClimbingSystem>();
  51.     }
  52.    
  53.     void Update ()
  54.     {
  55.         Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
  56.         Vector2 inputDir = input.normalized;
  57.         if (Input.GetButtonDown(sprintButton))
  58.         {
  59.             running = !running;
  60.         }
  61.         canClimb = climbingSystem.canClimb;
  62.         canBrace = climbingSystem.canBrace;
  63.  
  64.         if (!canClimb)
  65.         {
  66.             Move(inputDir, running);
  67.             gravity = -12;
  68.         }
  69.         else
  70.         {
  71.             Climb(inputDir);
  72.             gravity = 0;
  73.         }
  74.  
  75.         if (Input.GetButtonDown(jumpButton))
  76.         {
  77.             jumping = true;
  78.             crouching = false;
  79.             Jump();
  80.         }
  81.  
  82.         if (Input.GetButtonDown(crouchButton) && crouching == false)
  83.         {
  84.             crouching = !crouching;
  85.             Vector3 tVel = transform.position;
  86.             transform.position = Vector3.SmoothDamp(transform.position, new Vector3(0, (0 - controller.height) * 0.5f, 0), ref tVel, 0.2f);
  87.             running = false;
  88.         }
  89.         else if (Input.GetButtonDown(crouchButton) && crouching == true)
  90.         {
  91.             crouching = !crouching;
  92.             Vector3 tVel = transform.position;
  93.             transform.position = Vector3.SmoothDamp(transform.position, new Vector3(0, (controller.height), 0), ref tVel, 0.2f);
  94.         }
  95.  
  96.         animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
  97.         if (animationSpeedPercent < 0.3f)
  98.         {
  99.             walkSpeed = startWalkSpeed;
  100.             running = false;
  101.         }
  102.         animator.SetFloat("GrabbingX", input.x, speedSmoothTime, Time.deltaTime);
  103.         animator.SetFloat("speedPercent", targetSpeed, speedSmoothTime, Time.deltaTime);
  104.         animator.SetBool("Jumping", jumping);
  105.         animator.SetBool("Crouching", crouching);
  106.         animator.SetBool("Climbing", canClimb);
  107.         animator.SetBool("CanBrace", canBrace);
  108.     }
  109.  
  110.     private void Move(Vector2 inputDir, bool running)
  111.     {
  112.         if (inputDir != Vector2.zero)
  113.         {
  114.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.transform.eulerAngles.y;
  115.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
  116.         }
  117.         if (!crouching)
  118.         {
  119.             controller.height = Mathf.Lerp(controller.height, normalCharacterHeight, heightSmoothTime * Time.deltaTime);
  120.             //Gain speed with time
  121.             if (walkSpeed < maxWalkSpeed)
  122.             {
  123.                 walkSpeed += Time.deltaTime * acceleration;
  124.             }
  125.             else
  126.             {
  127.                 walkSpeed = maxWalkSpeed;
  128.             }
  129.             acceleration = (running) ? maxAcceleration : startAcceleration;
  130.             targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
  131.         }
  132.         else
  133.         {  
  134.             controller.height = Mathf.Lerp(controller.height, crouchCharacterHeight, heightSmoothTime);
  135.             targetSpeed = crouchSpeed * inputDir.magnitude;
  136.         }
  137.  
  138.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
  139.  
  140.         velocityY += Time.deltaTime * gravity;
  141.  
  142.         Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
  143.  
  144.         controller.Move(velocity * Time.deltaTime);
  145.         currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
  146.  
  147.         if (controller.isGrounded)
  148.         {
  149.             jumping = false;
  150.             controller.height = Mathf.Lerp(controller.height, normalCharacterHeight, heightSmoothTime);
  151.             velocityY = 0;
  152.         }
  153.     }
  154.  
  155.     private void Climb(Vector2 inputDir)
  156.     {
  157.         gravity = 0;
  158.         currentSpeed = walkSpeed * 0.5f;
  159.         Vector3 velocity = Vector3.zero;
  160.         yOffset = (canBrace) ? bracedYOffset : hangingYOffset;
  161.         Vector3 newPos = transform.position + Vector3.up * yOffset;
  162.         transform.position = newPos;
  163.         velocity = transform.right * currentSpeed * inputDir.x + Vector3.up * gravity;
  164.         controller.Move(velocity * Time.deltaTime);
  165.         if (Input.GetButtonDown(jumpButton))
  166.         {
  167.             velocity = transform.right * 10 * inputDir.normalized.x + Vector3.up * jumpHeight;
  168.             controller.Move(velocity * Time.deltaTime);
  169.         }
  170.     }
  171.  
  172.     private void Jump()
  173.     {
  174.         if (controller.isGrounded)
  175.         {
  176.             float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
  177.             velocityY = jumpVelocity;
  178.             controller.height = Mathf.Lerp(controller.height, jumpCharacterHeight, heightSmoothTime);
  179.         }
  180.     }
  181.  
  182.     private float GetModifiedSmoothTime(float smoothTime)
  183.     {
  184.         if (controller.isGrounded)
  185.         {
  186.             return smoothTime;
  187.         }
  188.  
  189.         if (airControlPercent == 0)
  190.         {
  191.             return float.MaxValue;
  192.         }
  193.         return smoothTime / airControlPercent;
  194.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement