I am coding a 2d rpg and was adding a dash mechanic, but for some reason whenever the facing value is (-1, 0), (-1, -1), or (0, -1) the force doesn't get applied. It works perfectly fine in all other directions, and I have used debug.log to see the velocity output which is correct. it seems like it getting canceled out but I don't know what is causing it. Here is my code for adding the force.
if (Input.GetKeyDown(KeyCode.C) && !exhausted)
{
if (stamina >= dashStaminaCost)
{
staminaTimer = 0;
stamina -= dashStaminaCost;
body.AddForce(facing * dashForce, ForceMode2D.Impulse);
}
}
If you suggest that some of the facing directions cause improper application of force, this could imply that, in those instances, the computation for “facing” vector is wrong.
To ensure uniform force application no matter the way:
The facing vector should be normalized so it has a length of 1 unit.
You can normalize it with something like this: