- Home /
My dash is working infrequent. I tried AddForce and adding velocity but nothing changed the fact, that it wont work sometimes.
void FixedUpdate() {
//Dash
if (dashTime == -1)
{
if (playercoin.points >= 1 && Input.GetKeyDown("space"))
{
if (z == 1 && x == 0)
{
rb.AddForce(0,0,30000);
dashTime = startdashTime;
playercoin.points--;
dashp.enableEmission = true;
dashp.Play();
Ability.fillAmount = 0;
}
if (z == -1 && x == 0)
{
rb.velocity = Vector3.back * dashSpeed;
dashTime = startdashTime;
playercoin.points--;
dashp.enableEmission = true;
dashp.Play();
Ability.fillAmount = 0;
}
if (z == 0 && x == -1)
{
rb.velocity = Vector3.left * dashSpeed;
dashTime = startdashTime;
playercoin.points--;
dashp.enableEmission = true;
dashp.Play();
Ability.fillAmount = 0;
}
if (z == 0 && x == 1)
{
rb.AddForce(0, 0, -30000);
dashTime = startdashTime;
playercoin.points--;
dashp.enableEmission = true;
dashp.Play();
Ability.fillAmount = 0;
}
}
}
else
{
rb.velocity = Vector2.zero;
dashTime -= Time.deltaTime;
Ability.fillAmount -= 1 / startdashTime * Time.deltaTime;
}
Answer by ChocoboyYT · Feb 09, 2021 at 05:32 AM
I had this problem before. All you have to do is change FixedUpdate to Update. Yes, this will make the game inconsistent between computers, but that's why you multiply all of your physics calculations with Time.deltaTime. So, your code should look like this
void Update() {
//Dash
if (dashTime == -1)
{
if (playercoin.points >= 1 && Input.GetKeyDown("space"))
{
if (z == 1 && x == 0)
{
rb.AddForce(0,0,30000 * Time.deltaTime);
dashTime = startdashTime;
playercoin.points--;
dashp.enableEmission = true;
dashp.Play();
Ability.fillAmount = 0;
}
if (z == -1 && x == 0)
{
rb.velocity = Vector3.back * dashSpeed;
dashTime = startdashTime;
playercoin.points--;
dashp.enableEmission = true;
dashp.Play();
Ability.fillAmount = 0;
}
if (z == 0 && x == -1)
{
rb.velocity = Vector3.left * dashSpeed * Time.deltaTime;
dashTime = startdashTime;
playercoin.points--;
dashp.enableEmission = true;
dashp.Play();
Ability.fillAmount = 0;
}
if (z == 0 && x == 1)
{
rb.AddForce(0, 0, -30000 * Time.deltaTime);
dashTime = startdashTime;
playercoin.points--;
dashp.enableEmission = true;
dashp.Play();
Ability.fillAmount = 0;
}
}
}
else
{
rb.velocity = Vector2.zero;
dashTime -= Time.deltaTime;
Ability.fillAmount -= 1 / startdashTime * Time.deltaTime;
}
The reason that FixedUpdate didn't work is that when you type "Input.GetKeyDown("space")", you are asking Unity, "Hey, on the frame that I press this, can you do this for me?" It works on Update because every frame is an ok from Unity, but if you use FixedUpdate, it is only an ok on the frames that FixedUpdate is active which is much different from Update.
Your answer
Follow this Question
Related Questions
Can Dash only once 2D 2 Answers
Getting a position behind a target object (targeted attack follow through) 2 Answers
dash left and right 1 Answer
Help with my Dash move? 1 Answer