Free 2d dash ability script!

I have a dashing mechanic in my game. I was using Transform, but since transform doesn’t account for physics I was going through walls on occasion. I’m trying to use addforce, but it doesn’t have the quick burst of speed effect as previously.

how can I use addforce to make the dash seem more like a dash?

I think my dash’s cooldown is messed up too, allowing the player to spam the button, instead of using it once, then having to wait. If someone could check that out it would help me alot too.

		if(Input.GetKey(KeyCode.LeftShift) && canIDash == true){
			if(direction == true)
				{
				dashTimer += Time.deltaTime*3;
				rigidbody.AddForce(Vector3.right*50);
				
			}
			if (direction == false)
				{
				dashTimer += Time.deltaTime*3;
				rigidbody.AddForce(Vector3.left*50);
					}
			}
		
			if(dashTimer > .5f)
				{
				canIDash = false;
				dashCooldown = true;
				}
			if(dashTimer < .5f && dashCooldown == false)
			{
				canIDash = true;
			}
			if(dashTimer <= 0)
			{
				dashCooldown = false;
			}
			
		}

EDIT:


This is what the script currently looks like, it still doesn’t work perfect. If anyone can help it would be awesome.

 public class DashAbility : MonoBehaviour {

 public float dashCooldown;
 public bool canIDash = true;
 public Vector2 savedVelocity;

 void Update () 
 {
     if(dashCooldown > 0)
     {
         canIDash = false;
         dashCooldown -= Time.deltaTime;
               //if I put saved velocity here, it will continue to move at savedVelocity until dashCooldown = 0? so it can't go here? right?
     }
     if(dashCooldown <= 0)
     {
         canIDash = true;
              //if I put savedVelocity here it doesn't return to savedVelocity until dashCooldown <=0 so... it doesn't go here either right...?
     }
     if(Input.GetKeyDown(KeyCode.LeftShift) && canIDash == true)
     {
         //saves velocity prior to dashing
         savedVelocity = rigidbody.velocity;
         //this part is the actual dash itself
         rigidbody.velocity =  new Vector2(rigidbody.velocity.x*3f, rigidbody.velocity.y);
         //sets up a cooldown so you have to wait to dash again
         dashCooldown = 2;
     }
 }

Ok, I’m pulling this out of the comment thread. From a technical perspective, your dash mechanic may be modeled as a state machine.

We really don’t need to get that complicated or formal in our implementation, but it might be helpful to think of dashing as a set of states (Ready, Dashing, Cooldown), with specific conditions that transition from state to state (is the dash key down?, is max dash hit?, is cooldown time over?).

Try out something like the following script. It uses an enum to represent the three states. Notes:

  • Dash can only be activated when you’re in the Ready state, and activating it sets the Dashing state. This prevents the dash button continuously attempting to save previous velocity.
  • For the other two cases, I added a bit of value coercion so the timer never goes outside of 0 or the max.

Let me know if you have any questions!

public class DashAbility : MonoBehaviour {
	
	public DashState dashState;
	public float dashTimer;
	public float maxDash = 20f;

	public Vector2 savedVelocity;
	
	void Update () 
	{
		switch (dashState) 
		{
		case DashState.Ready:
			var isDashKeyDown = Input.GetKeyDown (KeyCode.LeftShift);
			if(isDashKeyDown)
			{
				savedVelocity = rigidbody.velocity;
				rigidbody.velocity =  new Vector2(rigidbody.velocity.x * 3f, rigidbody.velocity.y);
				dashState = DashState.Dashing;
			}
			break;
		case DashState.Dashing:
			dashTimer += Time.deltaTime * 3;
			if(dashTimer >= maxDash)
			{
				dashTimer = maxDash;
				rigidbody.velocity = savedVelocity;
				dashState = DashState.Cooldown;
			}
			break;
		case DashState.Cooldown:
			dashTimer -= Time.deltaTime;
			if(dashTimer <= 0)
			{
				dashTimer = 0;
				dashState = DashState.Ready;
			}
			break;
		}
	}
}

public enum DashState 
{
	Ready,
	Dashing,
	Cooldown
}

Give this a shot:

rigidbody.AddForce(Vector3.right*50,  ForceMode.VelocityChange);

Check Force here at the Docs.