Advertisement
Pro_Unit

PlayerControllerNew

Sep 29th, 2021
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. using UnityEngine;
  5.  
  6. public class PlayerControllerNew : MonoBehaviour
  7. {
  8.     public bool walk { get; set; }
  9.    
  10.     [SerializeField] private CharacterController controller;
  11.  
  12.     [SerializeField] private Transform groundCheck;
  13.  
  14.     [SerializeField] private LayerMask groundMask;
  15.  
  16.     [SerializeField] private Transform cam;
  17.  
  18.     [SerializeField] private float speed;
  19.  
  20.     [SerializeField] private float turnSmothTime;
  21.  
  22.     [SerializeField] private float turnSmoothVelocity;
  23.  
  24.     [SerializeField] private float gravity = -9.8f;
  25.  
  26.     [SerializeField] private float jumpHeight = 3f;
  27.  
  28.     [SerializeField] private float groundDistance = 0.4f;
  29.  
  30.     [SerializeField] private float acceleration = 0.1f;
  31.  
  32.     [SerializeField] private float deceleration = 0.5f;
  33.  
  34.     private Vector3 velocity;
  35.  
  36.     private float velocityMove;
  37.  
  38.     private Animator anim;
  39.  
  40.     private bool isGround;
  41.  
  42.     private bool roll;
  43.  
  44.     private bool sneak;
  45.  
  46.     private bool fastRun;
  47.  
  48.     private static readonly int Speed = Animator.StringToHash("speed");
  49.     private static readonly int IsInAir = Animator.StringToHash("isInAir");
  50.     private static readonly int DualAttack = Animator.StringToHash("dualAttack");
  51.     private static readonly int Run = Animator.StringToHash("run");
  52.     private static readonly int Sneak = Animator.StringToHash("sneak");
  53.     private static readonly int JumpTrigger = Animator.StringToHash("jump");
  54.     private static readonly int Roll = Animator.StringToHash("roll");
  55.  
  56.     private bool NotRoll => roll == false;
  57.     private bool NotSneak => sneak == false;
  58.     private bool IsAir => isGround == false;
  59.  
  60.     private void Start() =>
  61.         anim = GetComponentInChildren<Animator>();
  62.  
  63.     private void Update()
  64.     {
  65.         isGround = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  66.  
  67.         if (isGround && velocity.y < 0)
  68.             velocity.y = -2f;
  69.  
  70.         velocity.y += gravity * Time.deltaTime;
  71.         controller.Move(velocity * Time.deltaTime);
  72.  
  73.         anim.SetBool(IsInAir, IsAir);
  74.  
  75.         bool jumpDown = Input.GetButtonDown("Jump");
  76.        
  77.         bool canJump = jumpDown &&
  78.                        isGround &&
  79.                        NotSneak &&
  80.                        NotRoll;
  81.        
  82.         if (canJump) // square
  83.             Jump();
  84.  
  85.         if (walk)
  86.             Walk();
  87.     }
  88.  
  89.     private void FixedUpdate()
  90.     {
  91.         if (Input.GetKey(KeyCode.JoystickButton7) && NotSneak) // R2
  92.         {
  93.             EnableAttack();
  94.             StartCoroutine(ExecuteAfterTime(2.5f, DisableAttack));
  95.         }
  96.  
  97.         if (Input.GetKey(KeyCode.JoystickButton1) && NotSneak) // circle
  98.         {
  99.             speed = 7;
  100.             anim.SetBool(Run, true);
  101.             StartCoroutine(ExecuteAfterTime(0.3f, EnableFastRun));
  102.         }
  103.  
  104.         else if (NotSneak)
  105.         {
  106.             speed = 5;
  107.             anim.SetBool(Run, false);
  108.             StartCoroutine(ExecuteAfterTime(0.3f, DisableFastRun));
  109.         }
  110.  
  111.         if (Input.GetKey(KeyCode.JoystickButton11)) // right mushroom
  112.         {
  113.             Vector3 tempCenter = controller.center;
  114.             tempCenter.y = controller.height / 2;
  115.             controller.center = tempCenter;
  116.             anim.SetBool(Sneak, true);
  117.             speed = 2;
  118.             sneak = true;
  119.         }
  120.         else
  121.         {
  122.             controller.height = 1.7f;
  123.             anim.SetBool(Sneak, false);
  124.             sneak = false;
  125.         }
  126.     }
  127.  
  128.     private void Jump()
  129.     {
  130.         anim.SetTrigger(JumpTrigger);
  131.         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  132.     }
  133.  
  134.     private void Walk()
  135.     {
  136.         float horizontal = Input.GetAxisRaw("Horizontal");
  137.         float vertical = Input.GetAxisRaw("Vertical");
  138.        
  139.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
  140.  
  141.         if (direction.magnitude >= 0.5f)
  142.         {
  143.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
  144.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity,
  145.                 turnSmothTime);
  146.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
  147.  
  148.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
  149.             controller.Move(moveDir.normalized * speed * Time.deltaTime);
  150.  
  151.             if (velocityMove < 1.0f)
  152.             {
  153.                 velocityMove += Time.deltaTime * acceleration;
  154.             }
  155.  
  156.             if (Input.GetKeyUp(KeyCode.JoystickButton1) && fastRun == false)
  157.             {
  158.                 EnableRoll();
  159.                 StartCoroutine(ExecuteAfterTime(1f, DisableRoll));
  160.             }
  161.         }
  162.         else
  163.         {
  164.             if (velocityMove > 0.0f)
  165.             {
  166.                 velocityMove -= Time.deltaTime * deceleration;
  167.             }
  168.         }
  169.  
  170.         anim.SetFloat(Speed, velocityMove);
  171.     }
  172.  
  173.     private void EnableFastRun() =>
  174.         fastRun = true;
  175.  
  176.     private void DisableFastRun() =>
  177.         fastRun = false;
  178.  
  179.     private void EnableAttack() =>
  180.         anim.SetBool(DualAttack, true);
  181.  
  182.     private void DisableAttack() =>
  183.         anim.SetBool(DualAttack, false);
  184.  
  185.     private void EnableRoll()
  186.     {
  187.         roll = true;
  188.         anim.SetBool(Roll, true);
  189.     }
  190.  
  191.     private void DisableRoll()
  192.     {
  193.         anim.SetBool(Roll, false);
  194.         roll = false;
  195.     }
  196.  
  197.     private IEnumerator ExecuteAfterTime(float timeInSec, Action action)
  198.     {
  199.         yield return new WaitForSeconds(timeInSec);
  200.         action?.Invoke();
  201.         anim.SetBool(DualAttack, false);
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement