So I am making a 2D top down game where player can move in any direction and face toward the mouse cursor. I am trying to use AddForce to make the character dash in his facing direction but nothing happen if I press the dash button.
void Update(){
if(eCooldownLeft<=0)
{
if(Input.GetKeyDown(KeyCode.E))
{
eCooldownLeft = eCooldown; //set cooldown upon activation
rb.AddForce(Vector2.up*dashForce, ForceMode2D.Impulse);
}
}
else eCooldownLeft -= Time.deltaTime; //cooldown countdown
}
Here is the movement script for the character:
void Update(){
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
mousePos = cam.ScreenToWorldPoint(Input.mousePosition); //get mouse position
}
void FixedUpdate() { //make character faces toward mouse position
rb.MovePosition(rb.position+ movement * speed * Time.fixedDeltaTime);
lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y,lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
I can use AddForce to projectiles and it works perfectly but I can’t do anything about adding force to the character. Am I doing anything wrong? Hope you guys can help. Thanks