How To Dash In Unity 2D Unity Player Movement 2D

How To Dash In Unity 2D | Unity Player Movement 2D

How To Dash In Unity 2D | Unity Player Movement 2D :- In this unified tutorial, we cover essential techniques for enhancing 2D player controls in Unity. First, dive into Unity Player Movement 2D to master the fundamentals of smooth and intuitive movement mechanics for your 2D game. Then, explore our 2D Unity Dash Tutorial to learn how to implement a swift and engaging dash feature, giving your character a burst of speed. Whether you’re just starting out or looking to polish your skills, this video provides a clear, step-by-step approach to creating responsive and exciting player controls. Tune in to boost your 2D game development skills and bring your game mechanics to life!

Unity Dash Script

using System.Collections;
using UnityEngine;

public class CharacterDashMovement : MonoBehaviour
{
    private float horizontal;
    private float speed = 8f;
    private float jumpingPower = 16f;
    private bool isFacingRight = true;

    private bool canDash = true;
    private bool isDashing;
    private float dashingPower = 24f;
    private float dashingTime = 0.2f;
    private float dashingCooldown = 1f;

    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;
    [SerializeField] private TrailRenderer tr;

    private void Update()
    {
        if (isDashing)
        {
            return;
        }

        horizontal = Input.GetAxisRaw("Horizontal");

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
        }

        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }

        if (Input.GetKeyDown(KeyCode.LeftShift) && canDash)
        {
            StartCoroutine(Dash());
        }

        Flip();
    }

    private void FixedUpdate()
    {
        if (isDashing)
        {
            return;
        }

        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 Dash()
    {
        canDash = false;
        isDashing = true;
        float originalGravity = rb.gravityScale;
        rb.gravityScale = 0f;
        rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f);
        tr.emitting = true;
        yield return new WaitForSeconds(dashingTime);
        tr.emitting = false;
        rb.gravityScale = originalGravity;
        isDashing = false;
        yield return new WaitForSeconds(dashingCooldown);
        canDash = true;
    }
}

***CHECK FULL VIDEO TUTORIAL***

Visit www.UnitySourceCode.store

Leave a Reply

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping