Unity 2D Dash Tutorial | Unity Tutorial
Learn how to implement a sleek 2D dash mechanic in Unity with our quick and easy tutorial! We’ll guide you through the entire process—from setting up input controls to fine-tuning the dash effect—to make your character’s movement smooth and dynamic. Perfect for both beginners and experienced developers, this video will help you add an exciting dash feature that enhances gameplay and keeps players engaged. Join us and take your 2D game to the next level!
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
Unity Projects - ON SALE!!!
1. Water Sort Puzzle 9000 Levels
2. Ball Sort Puzzle 9000 Levels
3. Sling Shot
=========================================================
Script:
PlayerController.cs
using System.Collections;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private float movementInput;
public float walkSpeed = 8f;
public float jumpStrength = 16f;
private bool facingRight = true;
private bool canPerformDash = true;
private bool isCurrentlyDashing;
private float dashStrength = 24f;
private float dashDuration = 0.2f;
private float dashCooldownTime = 1f;
private float dashInvincibilityDuration = 0.2f;
[SerializeField] private Rigidbody2D playerRigidbody;
[SerializeField] private Transform groundChecker;
[SerializeField] private LayerMask groundMask;
[SerializeField] private TrailRenderer dashTrail;
[SerializeField] private Animator characterAnimator;
[SerializeField] private AudioSource dashAudio;
private void Update()
{
if (isCurrentlyDashing)
{
return;
}
movementInput = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && CheckIfGrounded())
{
playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, jumpStrength);
}
if (Input.GetButtonUp("Jump") && playerRigidbody.velocity.y > 0f)
{
playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, playerRigidbody.velocity.y * 0.5f);
}
if (Input.GetKeyDown(KeyCode.LeftShift) && canPerformDash)
{
StartCoroutine(ExecuteDash());
}
HandleCharacterFlip();
}
private void FixedUpdate()
{
if (isCurrentlyDashing)
{
return;
}
playerRigidbody.velocity = new Vector2(movementInput * walkSpeed, playerRigidbody.velocity.y);
}
private bool CheckIfGrounded()
{
return Physics2D.OverlapCircle(groundChecker.position, 0.2f, groundMask);
}
private void HandleCharacterFlip()
{
if (facingRight && movementInput < 0f || !facingRight && movementInput > 0f)
{
Vector3 localScale = transform.localScale;
facingRight = !facingRight;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
private IEnumerator ExecuteDash()
{
canPerformDash = false;
isCurrentlyDashing = true;
// Trigger dash animation
if (characterAnimator != null)
{
characterAnimator.SetTrigger("Dash");
}
// Play dash sound effect
if (dashAudio != null)
{
dashAudio.Play();
}
float initialGravity = playerRigidbody.gravityScale;
playerRigidbody.gravityScale = 0f;
// Determine dash direction based on character's facing direction
float dashDirection = facingRight ? 1f : -1f;
playerRigidbody.velocity = new Vector2(dashDirection * dashStrength, 0f);
// Activate trail effect
if (dashTrail != null)
{
dashTrail.emitting = true;
}
// Optional: Handle dash invincibility or other effects here
Collider2D[] nearbyEnemies = Physics2D.OverlapCircleAll(transform.position, 0.5f, LayerMask.GetMask("Enemy"));
foreach (var enemy in nearbyEnemies)
{
// Implement logic for interaction with enemies
}
yield return new WaitForSeconds(dashDuration);
// Deactivate trail effect
if (dashTrail != null)
{
dashTrail.emitting = false;
}
playerRigidbody.gravityScale = initialGravity;
isCurrentlyDashing = false;
yield return new WaitForSeconds(dashCooldownTime);
canPerformDash = true;
}
}