You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class MoverController : EnemyController {
  4. public Transform[] moveRoute;
  5. public float moveSpeed;
  6. public float moveCoolDown = 2;
  7. public int routeLoc = 0;
  8. private int routeMax;
  9. private bool isMove = false;
  10. private float lastMoveTime = 0.5f;
  11. // Use this for initialization
  12. protected override void Start () {
  13. base.Start();
  14. routeMax = moveRoute.Length;
  15. transform.position = moveRoute[routeLoc % routeMax].position;
  16. }
  17. // Update is called once per frame
  18. void FixedUpdate () {
  19. if (Time.time - lastShotTime >= shotCoolDown) {
  20. lastShotTime = Time.time;
  21. Vector3 dir1 = (moveRoute[(routeLoc + 1) % routeMax].position - moveRoute[routeLoc % routeMax].position).normalized;
  22. Vector3 dir2 = (moveRoute[mod ((routeLoc - 1), routeMax)].position - moveRoute[routeLoc % routeMax].position).normalized;
  23. shoot(dir1);
  24. shoot(dir2);
  25. }
  26. if (Time.time - lastMoveTime >= moveCoolDown) {
  27. lastMoveTime = Time.time;
  28. StartCoroutine(moveLocation(moveSpeed));
  29. }
  30. }
  31. private IEnumerator moveLocation(float time) {
  32. Vector3 start = moveRoute[routeLoc % routeMax].position;
  33. Vector3 end = moveRoute[(routeLoc + 1 ) % routeMax].position;
  34. float elapsedTime = 0;
  35. Vector3 pos = start;
  36. while ((elapsedTime / time) <= 1) {
  37. pos = Vector3.Lerp(start, end, (elapsedTime / time));
  38. transform.position = pos;
  39. elapsedTime += Time.deltaTime;
  40. yield return new WaitForEndOfFrame();
  41. }
  42. transform.position = end;
  43. routeLoc++;
  44. }
  45. //c# modulo deals with negatives differently because it is a remainder call
  46. //this fixes that
  47. private int mod(int x, int m) {
  48. return (x % m + m) % m;
  49. }
  50. }