Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class BanalnaPlatformowkaMovement : MonoBehaviour
- {
- [Header("Movement")]
- [Range(1f, 20f)] public float moveSpeed = 5f;
- float horizontalMovement;
- [Header("Acceleration/Deceleration")]
- [Range(1f, 100f)] public float acceleration = 20f;
- [Range(1f, 100f)] public float deceleration = 25f;
- float currentSpeed = 0f;
- [Header("Jumping")]
- [Range(1f, 30f)] public float jumpForce = 5f;
- [Range(0.1f, 0.75f)] public float jumpCut = 0.3f;
- [SerializeField] int maxJumps = 2;
- [SerializeField] int maxExtraJumps = 1;
- int jumpsLeft;
- [Header("Coyote Time")]
- [SerializeField] float coyoteTime = 0.2f;
- float coyoteTimeCounter;
- [Header("Jump Buffer")]
- [SerializeField] float jumpBufferTime = 0.2f;
- float jumpBufferCounter;
- [Header("WallCheck")]
- public Transform wallCheckPos;
- public Vector2 wallCheckSize = new Vector2(0.49f, 0.03f);
- public LayerMask wallLayer;
- [Header("WallMovement")]
- [Range(1f, 10f)] public float wallSlideSpeed = 2f;
- bool isWallSliding;
- // Wall Jumping
- bool isWallJumping;
- float wallJumpDirection;
- float wallJumpTime = 0.5f;
- float wallJumpTimer;
- [SerializeField] Vector2 wallJumpPower = new Vector2(5f, 5f);
- [Header("Gravity")]
- public float baseGravity = 2f;
- public float maxFallSpeed = 18f;
- public float fallhorizontalMovement = 2f;
- bool isGrounded;
- Rigidbody2D rb;
- BoxCollider2D feetCollider;
- #region Awake
- void Awake()
- {
- rb = GetComponent<Rigidbody2D>();
- feetCollider = GetComponent<BoxCollider2D>();
- }
- #endregion
- #region Update
- void Update()
- {
- GroundCheck();
- Gravity();
- TryJump();
- FlipSprite();
- WallSlide();
- WallJump();
- if (jumpBufferCounter > 0)
- {
- jumpBufferCounter -= Time.deltaTime;
- }
- }
- #endregion
- #region FixedUpdate
- void FixedUpdate()
- {
- float targetSpeed = horizontalMovement * moveSpeed;
- if (Mathf.Abs(horizontalMovement) > 0.01f)
- {
- currentSpeed = Mathf.MoveTowards(currentSpeed, targetSpeed, acceleration * Time.fixedDeltaTime);
- }
- else
- {
- currentSpeed = Mathf.MoveTowards(currentSpeed, 0f, deceleration * Time.fixedDeltaTime);
- }
- rb.velocity = new Vector2(currentSpeed, rb.velocity.y);
- }
- #endregion
- #region Move
- public void Move(InputAction.CallbackContext context)
- {
- horizontalMovement = context.ReadValue<Vector2>().x;
- }
- #endregion
- #region FlipSprite
- void FlipSprite()
- {
- if (Mathf.Abs(horizontalMovement) > 0.01f)
- {
- transform.localScale = new Vector2(Mathf.Sign(horizontalMovement), 1f);
- }
- }
- #endregion
- #region Jump
- public void Jump(InputAction.CallbackContext context)
- {
- if (context.performed)
- {
- jumpBufferCounter = jumpBufferTime;
- }
- else if (context.canceled && rb.velocity.y > 0)
- {
- rb.velocity = new Vector2(0f, rb.velocity.y * jumpCut);
- }
- if (context.performed && wallJumpTimer > 0f)
- {
- isWallJumping = true;
- rb.velocity = new Vector2(wallJumpDirection * wallJumpPower.x, wallJumpPower.y);
- wallJumpTimer = 0;
- Invoke(nameof(CancelWallJump), wallJumpTime + 0.1f);
- }
- }
- void TryJump()
- {
- if ((isGrounded || coyoteTimeCounter > 0f) && jumpBufferCounter > 0f)
- {
- rb.velocity = new Vector2(rb.velocity.x, jumpForce);
- coyoteTimeCounter = 0f;
- jumpBufferCounter = 0f;
- jumpsLeft = maxExtraJumps;
- }
- else if (jumpBufferCounter > 0f && jumpsLeft > 0)
- {
- rb.velocity = new Vector2(rb.velocity.x, jumpForce);
- jumpsLeft--;
- jumpBufferCounter = 0f;
- }
- }
- #endregion
- #region Gravity
- void Gravity()
- {
- if (rb.velocity.y < 0)
- {
- rb.gravityScale = baseGravity * fallhorizontalMovement;
- rb.velocity = new Vector2(rb.velocity.x, Mathf.Max(rb.velocity.y, -maxFallSpeed));
- }
- else
- {
- rb.gravityScale = baseGravity;
- }
- }
- #endregion
- #region GroundCheck
- void GroundCheck()
- {
- isGrounded = feetCollider.IsTouchingLayers(LayerMask.GetMask("Ziemia"));
- if (isGrounded)
- {
- coyoteTimeCounter = coyoteTime;
- }
- else
- {
- coyoteTimeCounter -= Time.deltaTime;
- }
- }
- #endregion
- void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.blue;
- Gizmos.DrawWireCube(wallCheckPos.position, wallCheckSize);
- }
- #region WallCheck
- bool WallCheck()
- {
- return Physics2D.OverlapBox(wallCheckPos.position, wallCheckSize, 0, wallLayer);
- }
- #endregion
- #region WallSlide
- void WallSlide()
- {
- if (!isGrounded && WallCheck() && horizontalMovement == Mathf.Sign(transform.localScale.x))
- {
- isWallSliding = true;
- rb.velocity = new Vector2(rb.velocity.x, Mathf.Max(rb.velocity.y, -wallSlideSpeed));
- jumpsLeft = maxExtraJumps;
- }
- else
- {
- isWallSliding = false;
- }
- }
- #endregion
- #region WallJump
- void WallJump()
- {
- if (isWallSliding)
- {
- isWallJumping = false;
- wallJumpDirection = -transform.localScale.x;
- wallJumpTimer = wallJumpTime;
- CancelInvoke(nameof(CancelWallJump));
- }
- else if (wallJumpTimer > 0f)
- {
- wallJumpTimer -= Time.deltaTime;
- }
- }
- void CancelWallJump()
- {
- isWallJumping = false;
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment