Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using UnityEngine;
- public class PlayerControllerNew : MonoBehaviour
- {
- public bool walk { get; set; }
- [SerializeField] private CharacterController controller;
- [SerializeField] private Transform groundCheck;
- [SerializeField] private LayerMask groundMask;
- [SerializeField] private Transform cam;
- [SerializeField] private float speed;
- [SerializeField] private float turnSmothTime;
- [SerializeField] private float turnSmoothVelocity;
- [SerializeField] private float gravity = -9.8f;
- [SerializeField] private float jumpHeight = 3f;
- [SerializeField] private float groundDistance = 0.4f;
- [SerializeField] private float acceleration = 0.1f;
- [SerializeField] private float deceleration = 0.5f;
- private Vector3 velocity;
- private float velocityMove;
- private Animator anim;
- private bool isGround;
- private bool roll;
- private bool sneak;
- private bool fastRun;
- private static readonly int Speed = Animator.StringToHash("speed");
- private static readonly int IsInAir = Animator.StringToHash("isInAir");
- private static readonly int DualAttack = Animator.StringToHash("dualAttack");
- private static readonly int Run = Animator.StringToHash("run");
- private static readonly int Sneak = Animator.StringToHash("sneak");
- private static readonly int JumpTrigger = Animator.StringToHash("jump");
- private static readonly int Roll = Animator.StringToHash("roll");
- private bool NotRoll => roll == false;
- private bool NotSneak => sneak == false;
- private bool IsAir => isGround == false;
- private void Start() =>
- anim = GetComponentInChildren<Animator>();
- private void Update()
- {
- isGround = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- if (isGround && velocity.y < 0)
- velocity.y = -2f;
- velocity.y += gravity * Time.deltaTime;
- controller.Move(velocity * Time.deltaTime);
- anim.SetBool(IsInAir, IsAir);
- bool jumpDown = Input.GetButtonDown("Jump");
- bool canJump = jumpDown &&
- isGround &&
- NotSneak &&
- NotRoll;
- if (canJump) // square
- Jump();
- if (walk)
- Walk();
- }
- private void FixedUpdate()
- {
- if (Input.GetKey(KeyCode.JoystickButton7) && NotSneak) // R2
- {
- EnableAttack();
- StartCoroutine(ExecuteAfterTime(2.5f, DisableAttack));
- }
- if (Input.GetKey(KeyCode.JoystickButton1) && NotSneak) // circle
- {
- speed = 7;
- anim.SetBool(Run, true);
- StartCoroutine(ExecuteAfterTime(0.3f, EnableFastRun));
- }
- else if (NotSneak)
- {
- speed = 5;
- anim.SetBool(Run, false);
- StartCoroutine(ExecuteAfterTime(0.3f, DisableFastRun));
- }
- if (Input.GetKey(KeyCode.JoystickButton11)) // right mushroom
- {
- Vector3 tempCenter = controller.center;
- tempCenter.y = controller.height / 2;
- controller.center = tempCenter;
- anim.SetBool(Sneak, true);
- speed = 2;
- sneak = true;
- }
- else
- {
- controller.height = 1.7f;
- anim.SetBool(Sneak, false);
- sneak = false;
- }
- }
- private void Jump()
- {
- anim.SetTrigger(JumpTrigger);
- velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
- }
- private void Walk()
- {
- float horizontal = Input.GetAxisRaw("Horizontal");
- float vertical = Input.GetAxisRaw("Vertical");
- Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
- if (direction.magnitude >= 0.5f)
- {
- float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
- float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity,
- turnSmothTime);
- transform.rotation = Quaternion.Euler(0f, angle, 0f);
- Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
- controller.Move(moveDir.normalized * speed * Time.deltaTime);
- if (velocityMove < 1.0f)
- {
- velocityMove += Time.deltaTime * acceleration;
- }
- if (Input.GetKeyUp(KeyCode.JoystickButton1) && fastRun == false)
- {
- EnableRoll();
- StartCoroutine(ExecuteAfterTime(1f, DisableRoll));
- }
- }
- else
- {
- if (velocityMove > 0.0f)
- {
- velocityMove -= Time.deltaTime * deceleration;
- }
- }
- anim.SetFloat(Speed, velocityMove);
- }
- private void EnableFastRun() =>
- fastRun = true;
- private void DisableFastRun() =>
- fastRun = false;
- private void EnableAttack() =>
- anim.SetBool(DualAttack, true);
- private void DisableAttack() =>
- anim.SetBool(DualAttack, false);
- private void EnableRoll()
- {
- roll = true;
- anim.SetBool(Roll, true);
- }
- private void DisableRoll()
- {
- anim.SetBool(Roll, false);
- roll = false;
- }
- private IEnumerator ExecuteAfterTime(float timeInSec, Action action)
- {
- yield return new WaitForSeconds(timeInSec);
- action?.Invoke();
- anim.SetBool(DualAttack, false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement