johnnygoodguy2000

Player

Oct 4th, 2025 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.61 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. using UnityEngine;
  5. using UnityEngine.InputSystem;
  6. using UnityEngine.XR;
  7.  
  8. public class Player : MonoBehaviour
  9. {
  10.     //states
  11.     public PlayerState currentState;
  12.     public PlayerIdleState idleState;
  13.     public PlayerJumpState jumpState;
  14.     public PlayerMoveState moveState;
  15.     public PlayerCrouchState crouchState;
  16.     public PlayerSlideState slideState;
  17.     public PlayerAttackState attackState;
  18.  
  19.  
  20.     [Header("Core Components")]
  21. public Combat combat;
  22.  
  23.     [Header("Components")]
  24.  
  25.     public Rigidbody2D rb;
  26.     public Animator anim;
  27.     public CapsuleCollider2D playerCollider;
  28.  
  29.     [Header("handles inputs")]
  30.     //inputs
  31.     public PlayerInput playerInput;
  32.     public Vector2 moveInput;
  33.     public bool runPressed;
  34.     public bool attackPressed;
  35.     public bool jumpPressed;
  36.     public bool jumpReleased;
  37.  
  38.    
  39.  
  40.     [Header("Movement variables")]
  41.  
  42.     public float walkSpeed;
  43.     public float runSpeed;
  44.     [Header("Jump variables")]
  45.     public float jumpForce;
  46.  
  47.     public float jumpCutMultiplier = 0.5f;
  48.  
  49.     public float normalGravity;
  50.     public float fallGravity;
  51.     public float jumpGravity;
  52.     public int faceDirection = 1;
  53.  
  54.    
  55.     [Header("Ground check")]
  56.  
  57.     public Transform groundCheck;
  58.     public float groundCheckRadius;
  59.     public LayerMask groundLayer;
  60.     public bool isGrounded;
  61.  
  62.     [Header("Crouch variables")]
  63.     public Transform headCheck;
  64.     public float headCheckRadius = 0.2f;
  65.    
  66.  
  67.  
  68. [Header("Slide Settings and variables")]
  69.     public float slideDuration = 0.6f;
  70.     public float slideSpeed = 12f;
  71.     public float slideStopDuration = 0.15f;
  72.  
  73.    
  74.  
  75.     public float slideHeight;
  76.     public Vector2 slideOffset;
  77.  
  78.     public float normalHeight;
  79.  
  80.     public Vector2 normalOffset;
  81.     [SerializeField] private bool isSliding;
  82.  
  83.    
  84.  
  85.  
  86.  
  87.  
  88.  
  89.     private void Awake()
  90.     {
  91.         idleState = new PlayerIdleState(this);
  92.         jumpState = new PlayerJumpState(this);
  93.         moveState = new PlayerMoveState(this);
  94.         crouchState = new PlayerCrouchState(this);
  95.         slideState = new PlayerSlideState(this);
  96.         attackState = new PlayerAttackState(this);
  97.     }
  98.  
  99.  
  100.     private void Start()
  101.     {
  102.         rb.gravityScale = normalGravity;
  103.         ChangeState(idleState);
  104.     }
  105.  
  106.  
  107.     void Update()
  108.     {
  109.         currentState.Update();
  110.        
  111.  
  112.  
  113.  
  114. if (!isSliding)
  115.         {
  116.             Flip();
  117.         }
  118.        
  119.  
  120.        
  121.         HandleAnimations();
  122.        
  123.    
  124.     }
  125.  
  126.  
  127.     void FixedUpdate()
  128.     {
  129.         currentState.FixedUpdate();
  130.        
  131.  
  132.         CheckGrounded();
  133.        
  134.     }
  135.     public void ChangeState(PlayerState newState)
  136.     {
  137.      
  138.     if (currentState != null)
  139.     {
  140.         currentState.Exit();
  141.     }
  142.  
  143.     currentState = newState;
  144.     if (currentState != null)
  145.     {
  146.         currentState.Enter();
  147.     }
  148. }
  149.  
  150.  
  151.     public void ApplyVariableGravity()
  152.     {
  153.         if (rb.linearVelocity.y < -0.1f) // falling
  154.         {
  155.             rb.gravityScale = fallGravity;
  156.         }
  157.         else if (rb.linearVelocity.y > 0.1f) //rising
  158.         {
  159.             rb.gravityScale = jumpGravity;
  160.         }
  161.         else
  162.         {
  163.             rb.gravityScale = normalGravity;
  164.         }
  165.     }
  166.  
  167.  
  168.     void CheckGrounded()
  169.     {
  170.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
  171.     }
  172.  
  173.  
  174.     public bool CheckForCeiling()
  175.     {
  176.         return Physics2D.OverlapCircle(headCheck.position, headCheckRadius, groundLayer);
  177.     }
  178.  
  179.     void HandleAnimations()
  180.     {
  181.        
  182.  
  183.        
  184.         anim.SetBool("isGrounded", isGrounded);
  185.        
  186.          
  187.          
  188.        
  189.        
  190.        
  191.  
  192.         anim.SetFloat("yVelocity", rb.linearVelocity.y);
  193.  
  194.        
  195.        
  196.        
  197.        
  198.      
  199.     }
  200.  
  201.  
  202.     void Flip()
  203.     {
  204.  
  205.         if (moveInput.x > 0.1f)
  206.         {
  207.             faceDirection = 1;
  208.         }
  209.         else if (moveInput.x < -0.1f)
  210.         {
  211.             faceDirection = -1;
  212.  
  213.  
  214.         }
  215.  
  216.         transform.localScale = new Vector3(faceDirection, 1, 1);
  217.     }
  218.  
  219.     public void AttackAnimationFinished()
  220.     {
  221.          currentState.AttackAnimationFinished();
  222.     }
  223.  
  224.  
  225.  
  226.  
  227.     public void OnMove(InputValue value)
  228.     {
  229.         moveInput = value.Get<Vector2>();
  230.     }
  231.  
  232.     public void OnRun(InputValue value)
  233.     {
  234.         runPressed = value.isPressed;
  235.     }
  236.    
  237.     public void OnAttack(InputValue value)
  238.     {
  239.         if (value.isPressed)
  240.         {
  241.             attackPressed = value.isPressed;
  242.            
  243.         }
  244.     }
  245.  
  246.     public void OnJump(InputValue value)
  247.     {
  248.         if (value.isPressed)
  249.  
  250.         {
  251.             jumpPressed = true;
  252.             jumpReleased = false;
  253.         }
  254.         else
  255.         {
  256.             jumpReleased = true;
  257.  
  258.  
  259.  
  260.         }
  261.  
  262.  
  263.     }
  264.     public void SetColliderNormal()
  265. {
  266.     if (playerCollider == null) playerCollider = GetComponent<CapsuleCollider2D>();
  267.     playerCollider.size   = new Vector2(playerCollider.size.x, normalHeight);
  268.     playerCollider.offset = normalOffset;
  269. }
  270.  
  271.     public void SetColliderSlide()
  272.     {
  273.         if (playerCollider == null) playerCollider = GetComponent<CapsuleCollider2D>();
  274.         playerCollider.size = new Vector2(playerCollider.size.x, slideHeight);
  275.         playerCollider.offset = slideOffset;
  276.     }
  277.  
  278.  
  279.     private void OnDrawGizmos()
  280.     {
  281.         Gizmos.color = Color.red;
  282.         Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
  283.  
  284.         Gizmos.color = Color.blue;
  285.         Gizmos.DrawWireSphere(headCheck.position, headCheckRadius);
  286.     }
  287. }
Tags: Player
Advertisement
Add Comment
Please, Sign In to add comment