IEnumerator PerformMove(Quaternion oldOrientation, Vector3 newPos) { GameController.Instance.SwitchAction(true); GetComponent <UnitUI>().ShowMarkers(false); StartCoroutine(PlayMoveSound(true)); var targetRotation = Quaternion.LookRotation(new Vector3(newPos.x, transform.position.y, newPos.z) - transform.position); targetRotation.eulerAngles = new Vector3(0, targetRotation.eulerAngles.y, targetRotation.eulerAngles.z); //this is offset for Jumpship Vector3 offset = transform.position + Vector3.up / 2; //this is only for the Grunt model, usually unit.Model is declared as Animator Animation anim = unit.Model.GetComponent <Animation>(); if (anim != null && anim.GetClipCount() > 1) { unit.Model.GetComponent <Animation>().CrossFade("walk_forward"); } while (Mathf.Abs(transform.rotation.eulerAngles.y - targetRotation.eulerAngles.y) > 1) { transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * (unit.RotationSpeed * 10)); if (unit.Type == Unit.UnitType.Jumpship) { transform.position = Vector3.MoveTowards(transform.position, offset, Time.deltaTime); } yield return(null); } while (Vector3.Distance(transform.position, newPos) > 0.01f) { transform.position = Vector3.MoveTowards(transform.position, newPos, Time.deltaTime * (unit.MovemenSpeed / 10)); yield return(null); } if (anim != null && anim.GetClipCount() > 1) { unit.Model.GetComponent <Animation>().CrossFade("idle"); } targetRotation = oldOrientation; while (Mathf.Abs(transform.rotation.eulerAngles.y - targetRotation.eulerAngles.y) > 1) { transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * (unit.RotationSpeed * 10)); yield return(null); } StartCoroutine(PlayMoveSound(false)); GameController.Instance.SwitchAction(false); if (unit.Type == Unit.UnitType.Drone) { GameController.Instance.CheckEndGame(); } UnitUI unitUI = GetComponent <UnitUI>(); if (!unit.CanAttack || GetComponent <UnitAttack>().GetAttackTiles(BoardManager.Instance.GetPositionOfUnit(unit), true, BoardManager.TileType.Attack).Count == 0) { unitUI.ShowActionMarker(false); unitUI.ShowTeamIndicator(true); } else { unitUI.ShowMarkers(true); } }
IEnumerator PerformAttack(Unit target) { GameController.Instance.SwitchAction(true); unit.CanAttack = false; GetComponent <UnitUI>().ShowMarkers(false); Transform rotSource = transform; Quaternion oldOrientation = transform.rotation; if (Turret != null) { rotSource = Turret; oldOrientation = Turret.rotation; } Vector3 oldPos = rotSource.position; var targetRotation = Quaternion.LookRotation(new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z) - rotSource.position); targetRotation.eulerAngles = new Vector3(0, targetRotation.eulerAngles.y, targetRotation.eulerAngles.z); //this is offset for Jumpship or Dreadnought Vector3 offset = rotSource.position + Vector3.up / 2; if (unit.Type == Unit.UnitType.Jumpship || unit.Type == Unit.UnitType.Dreadnought) { while (Vector3.Distance(rotSource.position, offset) > 0.2f) { rotSource.position = Vector3.MoveTowards(rotSource.position, offset, Time.deltaTime); yield return(null); } } while (Mathf.Abs(rotSource.rotation.eulerAngles.y - targetRotation.eulerAngles.y) > 1) { rotSource.rotation = Quaternion.RotateTowards(rotSource.rotation, targetRotation, Time.deltaTime * (unit.RotationSpeed * 10)); yield return(null); } //simulate loading time yield return(new WaitForSeconds(0.5f)); if (unit.Type == Unit.UnitType.Jumpship || unit.Type == Unit.UnitType.Dreadnought) { foreach (Unit enemy in Unit.UnitsList.Where(x => unit.IsPlayerControlled != x.IsPlayerControlled).ToList()) { if (GetAttackTiles(BoardManager.Instance.GetPositionOfUnit(unit), true, BoardManager.TileType.Attack).Contains(BoardManager.Instance.GetPositionOfUnit(enemy))) { FireProjectile(enemy); } } } else { FireProjectile(target); } targetRotation = oldOrientation; yield return(new WaitForSeconds(1f)); while (Mathf.Abs(rotSource.rotation.eulerAngles.y - targetRotation.eulerAngles.y) > 1) { rotSource.rotation = Quaternion.RotateTowards(rotSource.rotation, targetRotation, Time.deltaTime * (unit.RotationSpeed * 10)); yield return(null); } if (unit.Type == Unit.UnitType.Jumpship || unit.Type == Unit.UnitType.Dreadnought) { while (Vector3.Distance(rotSource.position, oldPos) > 0.01f) { rotSource.position = Vector3.MoveTowards(rotSource.position, oldPos, Time.deltaTime); yield return(null); } } GameController.Instance.SwitchAction(false); UnitUI unitUI = GetComponent <UnitUI>(); if (!unit.CanMove || GetComponent <UnitMovement>().GetMoveFields(BoardManager.TileType.Move).Count == 0) { unitUI.ShowActionMarker(false); unitUI.ShowTeamIndicator(true); } else { unitUI.ShowMarkers(true); } GameController.Instance.CheckEndGame(); }