private IEnumerator DelayAction() { yield return(null); knockedback = false; OnKnockbackFinished(); FinishedKnockback.Invoke(this); }
public void KnockbackOnDeath() { StopAllCoroutines(); willDieOnKnockback = false; knockedback = false; if (OnObstacleHit != null) { OnObstacleHit.Invoke(this); } if (FinishedKnockback != null) { FinishedKnockback.Invoke(this); } }
private IEnumerator KnockedBack(List <GridSpace> path, GridObject obstacle) { //Set up speed and last position Vector3 last = obstacle != null ? obstacle.transform.position : path[path.Count - 1].transform.position; float speed = obstacle != null ? knockbackSpeed : knockbackSpeed * 2; // Find the direction the object will be moving in Vector3 forward = last - transform.position; forward.y = 0; //Setup destination Vector3 destination = obstacle != null ? forward + (forward.normalized) : forward; destination += transform.position; bool hitObstacle = false; Vector3 target = transform.position; if (path.Count > 0) { target = path[0].transform.position; } else { hitObstacle = true; Vector3 dir = (obstacle.transform.position - gridSpace.transform.position); target = (dir / 2) + gridSpace.transform.position; } while (true) { Vector3 direction = target - transform.position; direction.y = 0; float angle = Vector3.Angle(direction, forward); if (direction.magnitude <= 0.05f || angle >= 30) { if (path.Count > 0) { UpdateGridSpace(path[0]); path.RemoveAt(0); } if (path.Count == 0) { if (obstacle == null) { if (gridSpace != null) { transform.position = gridSpace.transform.position; if (GetObjectType() == ObjectType.Unit && TryGetComponent(out EnemySuperClass enemy)) { enemy.FindAttackRange(); } } knockedback = false; OnKnockbackFinished(); if (FinishedKnockback != null) { FinishedKnockback.Invoke(this); } break; } else { if (!hitObstacle) { hitObstacle = true; Vector3 dir = (obstacle.transform.position - gridSpace.transform.position); target = (dir / 2) + gridSpace.transform.position; destination = dir * 2 + transform.position; } else if (obstacle != null) { HitObstacle(obstacle); obstacle = null; if (GetObjectType() == ObjectType.Unit && GetComponent <GameCharacter>().GetHealth() <= 0) { break; } target = gridSpace.transform.position; forward = gridSpace.transform.position - transform.position; destination = gridSpace.transform.position; } } } else { target = path[0].transform.position; } } else { float moveDelta = Mathf.Clamp01(speed * Time.deltaTime); Vector3 movement = Vector3.Lerp(transform.position, destination, moveDelta); transform.position = movement; yield return(null); } } }