Untitled
unknown
csharp
a year ago
3.8 kB
7
Indexable
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { private float horizontal; private float speed = 8f; private float jumpingPower = 16f; private bool isFacingRight = true; private bool isJumping; private bool canDoubleJump; private float coyoteTime = 0.2f; private float coyoteTimeCounter; private float jumpBufferTime = 0.2f; private float jumpBufferCounter; [SerializeField] private Rigidbody2D rb; [SerializeField] private Transform groundCheck; [SerializeField] private LayerMask groundLayer; [SerializeField] private TrailRenderer trail; [SerializeField] private bool enableDoubleJump = true; // New variable to enable/disable double jump private bool canDash = true; private bool isDashing; [SerializeField] private float dashingPower = 24f; [SerializeField] private float dashingTime = 0.2f; [SerializeField] private float dashingCooldown = 1f; private void Start() { } private void Update() { if(isDashing) { return; } horizontal = Input.GetAxisRaw("Horizontal"); if (IsGrounded()) { coyoteTimeCounter = coyoteTime; canDoubleJump = true; // Reset double jump when grounded } else { coyoteTimeCounter -= Time.deltaTime; } if (Input.GetButtonDown("Jump")) { jumpBufferCounter = jumpBufferTime; } else { jumpBufferCounter -= Time.deltaTime; } if (coyoteTimeCounter > 0f && jumpBufferCounter > 0f && !isJumping) { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); jumpBufferCounter = 0f; StartCoroutine(JumpCooldown()); } else if (enableDoubleJump && canDoubleJump && jumpBufferCounter > 0f && !isJumping) { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); jumpBufferCounter = 0f; canDoubleJump = false; // Disable further double jumps StartCoroutine(JumpCooldown()); } if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f) { rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); coyoteTimeCounter = 0f; } Flip(); //Dashing if(Input.GetKeyDown(KeyCode.LeftShift) && canDash) { StartCoroutine(Dash()); } } private void FixedUpdate() { rb.velocity = new Vector2(horizontal * speed, rb.velocity.y); } private bool IsGrounded() { return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer); } private void Flip() { if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f) { Vector3 localScale = transform.localScale; isFacingRight = !isFacingRight; localScale.x *= -1f; transform.localScale = localScale; } } private IEnumerator JumpCooldown() { isJumping = true; yield return new WaitForSeconds(0.4f); isJumping = false; } private IEnumerator Dash() { canDash = false; isDashing = true; float orignalGravity = rb.gravityScale; rb.gravityScale = 0f; rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f); trail.emitting = true; yield return new WaitForSeconds(dashingTime); trail.emitting = false; rb.gravityScale = orignalGravity; isDashing = false; yield return new WaitForSeconds(dashingCooldown); canDash = true; } }
Editor is loading...
Leave a Comment