Guest User

Untitled

a guest
Aug 4th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.17 KB | Software | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.InputSystem;
  6.  
  7. public class BanalnaPlatformowkaMovement : MonoBehaviour
  8. {
  9.     [Header("Movement")]
  10.     [Range(1f, 20f)] public float moveSpeed = 5f;
  11.     float horizontalMovement;
  12.  
  13.  
  14.     [Header("Acceleration/Deceleration")]
  15.     [Range(1f, 100f)] public float acceleration = 20f;
  16.     [Range(1f, 100f)] public float deceleration = 25f;
  17.     float currentSpeed = 0f;
  18.  
  19.  
  20.     [Header("Jumping")]
  21.     [Range(1f, 30f)] public float jumpForce = 5f;
  22.     [Range(0.1f, 0.75f)] public float jumpCut = 0.3f;
  23.     [SerializeField] int maxJumps = 2;
  24.     [SerializeField] int maxExtraJumps = 1;
  25.     int jumpsLeft;
  26.  
  27.  
  28.     [Header("Coyote Time")]
  29.     [SerializeField] float coyoteTime = 0.2f;
  30.     float coyoteTimeCounter;
  31.  
  32.  
  33.     [Header("Jump Buffer")]
  34.     [SerializeField] float jumpBufferTime = 0.2f;
  35.     float jumpBufferCounter;
  36.  
  37.  
  38.     [Header("WallCheck")]
  39.     public Transform wallCheckPos;
  40.     public Vector2 wallCheckSize = new Vector2(0.49f, 0.03f);
  41.     public LayerMask wallLayer;
  42.  
  43.  
  44.     [Header("WallMovement")]
  45.     [Range(1f, 10f)] public float wallSlideSpeed = 2f;
  46.     bool isWallSliding;
  47.  
  48.  
  49.     // Wall Jumping
  50.     bool isWallJumping;
  51.     float wallJumpDirection;
  52.     float wallJumpTime = 0.5f;
  53.     float wallJumpTimer;
  54.     [SerializeField] Vector2 wallJumpPower = new Vector2(5f, 5f);
  55.  
  56.  
  57.     [Header("Gravity")]
  58.     public float baseGravity = 2f;
  59.     public float maxFallSpeed = 18f;
  60.     public float fallhorizontalMovement = 2f;
  61.     bool isGrounded;
  62.  
  63.  
  64.     Rigidbody2D rb;
  65.     BoxCollider2D feetCollider;
  66.  
  67.  
  68.     #region Awake
  69.  
  70.     void Awake()
  71.     {
  72.         rb = GetComponent<Rigidbody2D>();
  73.         feetCollider = GetComponent<BoxCollider2D>();
  74.     }
  75.  
  76.     #endregion
  77.  
  78.     #region Update
  79.  
  80.     void Update()
  81.     {
  82.         GroundCheck();
  83.         Gravity();
  84.         TryJump();
  85.         FlipSprite();
  86.         WallSlide();
  87.         WallJump();
  88.  
  89.         if (jumpBufferCounter > 0)
  90.         {
  91.             jumpBufferCounter -= Time.deltaTime;
  92.         }
  93.     }
  94.  
  95.     #endregion
  96.  
  97.     #region FixedUpdate
  98.  
  99.     void FixedUpdate()
  100.     {
  101.         float targetSpeed = horizontalMovement * moveSpeed;
  102.  
  103.         if (Mathf.Abs(horizontalMovement) > 0.01f)
  104.         {
  105.             currentSpeed = Mathf.MoveTowards(currentSpeed, targetSpeed, acceleration * Time.fixedDeltaTime);
  106.         }
  107.         else
  108.         {
  109.             currentSpeed = Mathf.MoveTowards(currentSpeed, 0f, deceleration * Time.fixedDeltaTime);
  110.         }
  111.  
  112.         rb.velocity = new Vector2(currentSpeed, rb.velocity.y);
  113.     }
  114.  
  115.     #endregion
  116.  
  117.     #region Move
  118.  
  119.     public void Move(InputAction.CallbackContext context)
  120.     {
  121.         horizontalMovement = context.ReadValue<Vector2>().x;
  122.     }
  123.  
  124.     #endregion
  125.  
  126.     #region FlipSprite
  127.  
  128.     void FlipSprite()
  129.     {
  130.         if (Mathf.Abs(horizontalMovement) > 0.01f)
  131.         {
  132.             transform.localScale = new Vector2(Mathf.Sign(horizontalMovement), 1f);
  133.         }
  134.     }
  135.  
  136.     #endregion
  137.  
  138.     #region Jump
  139.  
  140.     public void Jump(InputAction.CallbackContext context)
  141.     {
  142.         if (context.performed)
  143.         {
  144.             jumpBufferCounter = jumpBufferTime;
  145.         }
  146.         else if (context.canceled && rb.velocity.y > 0)
  147.         {
  148.             rb.velocity = new Vector2(0f, rb.velocity.y * jumpCut);
  149.         }
  150.  
  151.         if (context.performed && wallJumpTimer > 0f)
  152.         {
  153.             isWallJumping = true;
  154.             rb.velocity = new Vector2(wallJumpDirection * wallJumpPower.x, wallJumpPower.y);
  155.             wallJumpTimer = 0;
  156.  
  157.             Invoke(nameof(CancelWallJump), wallJumpTime + 0.1f);
  158.         }
  159.     }
  160.  
  161.     void TryJump()
  162.     {
  163.         if ((isGrounded || coyoteTimeCounter > 0f) && jumpBufferCounter > 0f)
  164.         {
  165.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
  166.             coyoteTimeCounter = 0f;
  167.             jumpBufferCounter = 0f;
  168.             jumpsLeft = maxExtraJumps;
  169.         }
  170.         else if (jumpBufferCounter > 0f && jumpsLeft > 0)
  171.         {
  172.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
  173.             jumpsLeft--;
  174.             jumpBufferCounter = 0f;
  175.         }
  176.     }
  177.  
  178.     #endregion
  179.  
  180.     #region Gravity
  181.  
  182.     void Gravity()
  183.     {
  184.         if (rb.velocity.y < 0)
  185.         {
  186.             rb.gravityScale = baseGravity * fallhorizontalMovement;
  187.             rb.velocity = new Vector2(rb.velocity.x, Mathf.Max(rb.velocity.y, -maxFallSpeed));
  188.         }
  189.         else
  190.         {
  191.             rb.gravityScale = baseGravity;
  192.         }
  193.     }
  194.  
  195.     #endregion
  196.  
  197.     #region GroundCheck
  198.  
  199.     void GroundCheck()
  200.     {
  201.         isGrounded = feetCollider.IsTouchingLayers(LayerMask.GetMask("Ziemia"));
  202.  
  203.         if (isGrounded)
  204.         {
  205.             coyoteTimeCounter = coyoteTime;
  206.         }
  207.         else
  208.         {
  209.             coyoteTimeCounter -= Time.deltaTime;
  210.         }
  211.     }
  212.  
  213.     #endregion
  214.  
  215.     void OnDrawGizmosSelected()
  216.     {
  217.         Gizmos.color = Color.blue;
  218.         Gizmos.DrawWireCube(wallCheckPos.position, wallCheckSize);
  219.     }
  220.  
  221.     #region WallCheck
  222.  
  223.     bool WallCheck()
  224.     {
  225.         return Physics2D.OverlapBox(wallCheckPos.position, wallCheckSize, 0, wallLayer);
  226.     }
  227.  
  228.     #endregion
  229.    
  230.     #region WallSlide
  231.  
  232.     void WallSlide()
  233.     {
  234.         if (!isGrounded && WallCheck() && horizontalMovement == Mathf.Sign(transform.localScale.x))
  235.         {
  236.             isWallSliding = true;
  237.             rb.velocity = new Vector2(rb.velocity.x, Mathf.Max(rb.velocity.y, -wallSlideSpeed));
  238.  
  239.             jumpsLeft = maxExtraJumps;
  240.         }
  241.         else
  242.         {
  243.             isWallSliding = false;
  244.         }
  245.     }
  246.  
  247.     #endregion
  248.  
  249.  
  250.     #region WallJump
  251.  
  252.     void WallJump()
  253.     {
  254.         if (isWallSliding)
  255.         {
  256.             isWallJumping = false;
  257.             wallJumpDirection = -transform.localScale.x;
  258.             wallJumpTimer = wallJumpTime;
  259.  
  260.             CancelInvoke(nameof(CancelWallJump));
  261.         }
  262.         else if (wallJumpTimer > 0f)
  263.         {
  264.             wallJumpTimer -= Time.deltaTime;
  265.         }
  266.     }
  267.  
  268.     void CancelWallJump()
  269.     {
  270.         isWallJumping = false;
  271.     }
  272.  
  273.     #endregion
  274. }
Advertisement
Add Comment
Please, Sign In to add comment