Im building a 2d platformer, and trying to add a dash that will push the character in the direction he is facing (horizontally).
I tried to use addforce which did’nt work and also tried to change the character’s velocity but it did’nt work as well
it seems that the code that makes my character move is interupting the dash code (the addforce and the velocity change worked well without it)
here’s the code that controls the player’s movement:
Since you are setting the velocity directly for moving the player when you add the dash in the next frame the velocity is getting set to the regular velocity.
What you can do is set a variable for dash and check if it is false and then set the regular velocity using the code provided above.
And you set your isDashing variable to true when you add the dash to the player. Also you can set the isDashing variable to false again either based on time or if it reaches to certain velocity.
private bool canDash;
...
//assuming you are doing the dash with a input
void Update()
{
...
if (Input.GetKeyDown("D")) //D for dash?
{
//inAir would be just a bool that sais whether you are jumping
if (canDash == true && inAir == true)
{
//add force to the player
... your force code
//set canDash to false
canDash = false;
}
}
}
...
void OnColliderEnter(Collider col)
{
...
if (col.gameObject.tag == "ground")
{
if (canDash == false)
canDash = true;
//now we can dash since we are on the ground
}
}