Making a smooth dash in Unity 2D

Hi,

I hope this is a right spot for this kind of question, if it’s not then I’m sorry.

My question is about how to make a smooth dash like feature in Unity, for a 2D game. The result I would like to get is similar to like Megaman airdash etc.

The setting for the dharacter is that he is in free fall, and the dash function would give the player more ways to manouver in the game and avoid obstacles as he falls.

What I have managed to do is sort of make him dash around, but it looks more like a teleport at the moment, not a bad feature but doesnt fit the setting and it’s of course too janky.

Here is the part of the script that handles moving (hope this is ok to put the code here like this, figured it’s so small part and fairly simple). That Flip-function just simply manipulates local scale and reverses the sprite-character when he turns. The actual dash I’ve been trying to do is at the end of the code, where there is if-statement and keycode.space there.:

speed is the variable for normal movement before free fall starts, and boost variable is for the dash mechanic. I’ve been trying with and without ForceMode.Impulse, I’ve tried to apply Time.deltaTime in there in hopes it would smooth it, but no luck and I cant figure out whats wrong.

Mass of the character is 1, and linear drag is 0.7 ( I want him to reach custom terminal velocity fast and keep the fall speed constant)

void FixedUpdate ()
{
    float move = Input.GetAxis("Horizontal");
    rb.velocity = new Vector2(move * speed, rb.velocity.y);

    if (move > 0 && !facingRight)
    {
        Flip();
    }
    else if(move < 0 && facingRight)
    {
        Flip();
    }

    if (move > 0 && Input.GetKeyDown(KeyCode.Space))
    {
        rb.AddForce(new Vector2(boost, 0), ForceMode2D.Impulse);
    }
    else if (move < 0 && Input.GetKeyDown(KeyCode.Space))
    {
        rb.AddForce(new Vector2(-boost, 0), ForceMode2D.Impulse);
    }

Could someone please help a noob out :slight_smile:

Hi bro do you find the solution i have one but is not working good

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Dash1 : MonoBehaviour {

	public float dash = 3000f;
	public bool DashTime = true ;

	// Use this for initialization
	void Start () {
		bool DashTime = true;
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetButtonDown ("Dash")) 
		{
				GetComponent<Rigidbody2D> ().AddForce (new Vector2 (dash, 0), ForceMode2D.Force);
				StartCoroutine (LateCall ());
		}
	}
	IEnumerator LateCall()
	{
		yield return new WaitForSeconds(0.05f);
		GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.FreezePositionX;

		yield return new WaitForSeconds(0.06f);
		GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.None;
	}
}

Privacy & Terms