I want my character to dodge, so I was thinking of applying a force over time.
When the “dodge” button is pressed I start a coroutine that it should ensure that nothing will be executed when I enter the coroutine.
In the coroutine I do this:
private IEnumerator Dodge(Vector3 moveInput)
{
float dodgeTime = 2.0f;
// Differentiate between moving dodge and standing dodge
if (moveInput.sqrMagnitude > 0)
{
while (dodgeTime > 0)
{
GetComponent<Rigidbody>().AddForce(transform.forward, ForceMode.VelocityChange);
dodgeTime -= Time.fixedDeltaTime;
}
}
else
{
while (dodgeTime > 0)
{
GetComponent<Rigidbody>().AddForce(-transform.forward, ForceMode.VelocityChange);
dodgeTime -= Time.fixedDeltaTime;
}
}
yield return null;
}
The problem is that the force is applied outright. I’d like more to have a transition, to see the character actually moving on the ground and not just teleporting from one position to another. Because then I want to start an animation for the actual dodge. I tried to change the ForceMode, but nothing changed.
What am I doing wrong here?
Thanks guys!