Hi!
I would like to create a slime enemy movement, like a jumping/dashing one. If the target is in aggro range, but not in dashing range, just move towards, and if it’s close enough, then dash. There would be a preparing phase as well, so the player can see when the slime wants to dash. Here is the code for it:
private bool isDashing;
private bool canDash;
private bool isPreparingForDash;
void Update()
{
float dist = Vector3.Distance(_enemyManager.target.transform.position, transform.position);
if (dist <= _enemyManager.enemyRange && canDash)
{
StartCoroutine(DashMovementHandler());
if (isDashing && !isPreparingForDash)
transform.position = Vector2.MoveTowards(transform.position, _enemyManager.target.transform.position, Time.deltaTime * charStats.CurrentMoveSpeed * 10);
}
else
{
transform.position = Vector2.MoveTowards(transform.position, _enemyManager.target.transform.position, Time.deltaTime * charStats.CurrentMoveSpeed * 2);
}
}
IEnumerator DashMovementHandler()
{
canDash = true;
isPreparingForDash = true;
yield return new WaitForSeconds(0.5f);
isDashing = true;
isPreparingForDash = false;
yield return new WaitForSeconds(0.2f);
canDash = false;
isDashing = false;
yield return new WaitForSeconds(4f);
canDash = true;
}
It does stop, and even the booleans are good, and it should dash, but it’s just stop, and after 0.7 seconds, when the canDash goes back to false, it starts run again. And I don’t know why, because it should do the dash.
Can anyone see the issue?