public float dodgeSpeed = 10f;
public float dodgeDuration = 0.2f;
public float dodgeCooldown = 1f;
private Rigidbody2D rb;
private Vector2 dodgeDirection;
private bool isDodging = false;
private float dodgeTimer = 0f;
private float dodgeDurationTimer = 0f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (!isDodging && dodgeTimer <= 0f)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
StartDodge();
}
}
}
void FixedUpdate()
{
if (isDodging)
{
rb.MovePosition(rb.position + dodgeDirection.normalized * dodgeSpeed * Time.fixedDeltaTime);
dodgeDurationTimer += Time.fixedDeltaTime;
if (dodgeDurationTimer >= dodgeDuration)
{
EndDodge();
}
}
if (dodgeTimer > 0f)
{
dodgeTimer -= Time.fixedDeltaTime;
}
}
void StartDodge()
{
isDodging = true;
dodgeDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
dodgeDurationTimer = 0f;
if (dodgeDirection.magnitude > 0f)
{
dodgeDirection.Normalize();
}
else
{
dodgeDirection = Vector2.up;
}
rb.velocity = Vector2.zero;
StartCoroutine(EndDodgeAfterDuration());
}
void EndDodge()
{
isDodging = false;
dodgeTimer = dodgeCooldown;
}
IEnumerator EndDodgeAfterDuration()
{
yield return new WaitForSeconds(dodgeDuration + 0.05f);
EndDodge();
}
All of it works, it “Dodges”, the timer works and the duration is kept track of. It just doesn’t move. I bet it’s really obvious and I’m dumb but I can’t figure it out. Please help. Thank you