Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerController : MonoBehaviour
- {
- public float dashSpeed = 10f; // the speed at which the player will dash
- public float dashDuration = 0.5f; // the duration of the dash in seconds
- public float dashCooldown = 1f; // the cooldown period between dashes in seconds
- private Rigidbody2D rb;
- private bool isDashing = false; // flag to indicate if the player is currently dashing
- private float dashTimeLeft = 0f; // the remaining duration of the dash
- private float dashCooldownTimeLeft = 0f; // the remaining time until the player can dash again
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- }
- void Update()
- {
- // check if the player can dash
- if (Input.GetKeyDown(KeyCode.Space) && !isDashing && dashCooldownTimeLeft <= 0f)
- {
- isDashing = true;
- dashTimeLeft = dashDuration;
- rb.velocity = transform.right * dashSpeed;
- }
- // update the dash time and cooldown time
- if (isDashing)
- {
- dashTimeLeft -= Time.deltaTime;
- if (dashTimeLeft <= 0f)
- {
- isDashing = false;
- dashCooldownTimeLeft = dashCooldown;
- rb.velocity = Vector2.zero;
- }
- }
- else if (dashCooldownTimeLeft > 0f)
- {
- dashCooldownTimeLeft -= Time.deltaTime;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment