rb.velocity at update works strange (Begginer Question)

I want to ask a question for better understanding of unity. I have made dash for platfromer game, here is the code for dash and could you please explain why next code dont work

 void Update {
        if (Input.GetKeyDown(KeyCode.E))
        {
            isDashing = true;
            currentDashTimer = dashTimer;
            rb.velocity = Vector2.zero;
        }
        if (isDashing){
        Dash();
        }
}
public void Dash() {if (currentDashTimer <= 0)
        {
            isDashing = false;
        }
        else if (facingRight)
        {
            rb.velocity = Vector2.right * dashForce;
        } 
        else if (!facingRight)
        {
            rb.velocity = Vector2.left * dashForce;
        }
        currentDashTimer -= Time.deltaTime;
    } 

but this works

void Update {
        if (Input.GetKeyDown(KeyCode.E))
        {
            isDashing = true;
            currentDashTimer = dashTimer;
            rb.velocity = Vector2.zero;
        }

}
public void FixedUpdate(){
    if (isDashing){
    Dash();
    }
} 
public void Dash() {if (currentDashTimer <= 0)
        {
            isDashing = false;
        }
        else if (facingRight)
        {
            rb.velocity = Vector2.right * dashForce;
        } 
        else if (!facingRight)
        {
            rb.velocity = Vector2.left * dashForce;
        }
        currentDashTimer -= Time.deltaTime;
    }

So if I call Dash() at Update() it will not actually work, SO if I change dashForce it will not affect anything even if I add Time.fixedDeltaTime, but in second example where Dash() is called at FixedUpdate() everything works fine. Why? I just havew this question because, I have a Move() function that is similiar to Dash() But move works properly at Update() and FixedUpdate()



Read more here: https://stackoverflow.com/questions/66482258/rb-velocity-at-update-works-strange-begginer-question

Content Attribution

This content was originally published by V.D. at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: