Robot.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using DG.Tweening;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class Robot : MonoBehaviour
  6. {
  7. public float Speed = 1f;
  8. private Animation mAnimation;
  9. private Animator mAnimator;
  10. private string stateName;
  11. private bool isWalk;
  12. private string robotState;
  13. /*
  14. * Waving
  15. * Point_left
  16. */
  17. private void Start()
  18. {
  19. mAnimator = GetComponent<Animator>();
  20. StateOne();
  21. isWalk = false;
  22. }
  23. private void Update()
  24. {
  25. if(isWalk)
  26. {
  27. transform.Translate(Vector3.forward * Time.deltaTime * Speed,Space.Self);
  28. }
  29. }
  30. private void OnTriggerEnter(Collider other)
  31. {
  32. if(other.tag == "RobotBox")
  33. {
  34. switch (other.name)
  35. {
  36. case "RobotOne":
  37. transform.DOLocalRotate(new Vector3(0, 90, 0), 1f);
  38. StartCoroutine(Timing(1.5f, "Point_left"));
  39. break;
  40. case "RobotTwo":
  41. transform.DOLocalRotate(new Vector3(0, 90, 0), 1f);
  42. StartCoroutine(Timing(1.5f, "Point_right"));
  43. break;
  44. case "RobotThree":
  45. transform.DOLocalRotate(new Vector3(0, 90, 0), 1f);
  46. StartCoroutine(Timing(1.5f, "Point_left"));
  47. break;
  48. case "RobotFour":
  49. transform.DOLocalRotate(new Vector3(0, 90, 0), 1f);
  50. StartCoroutine(Timing(1.5f, "Point_left"));
  51. break;
  52. default:
  53. break;
  54. }
  55. robotState = other.name;
  56. }
  57. }
  58. private void StateOne()
  59. {
  60. CutState("Waving");
  61. StartCoroutine(Timing(1.5f, "Point_left"));
  62. StartCoroutine(StateTwo(4f));
  63. }
  64. IEnumerator StateTwo(float times)
  65. {
  66. yield return new WaitForSeconds(times);
  67. transform.DOLocalRotate(new Vector3(0, -90, 0), 1f);
  68. StartCoroutine(Timing(1f, "Walk"));
  69. // 转身
  70. // 行走, 进入下一个待定位置
  71. }
  72. /// <summary>
  73. /// 定时切换状态
  74. /// </summary>
  75. /// <param name="times"></param>
  76. /// <param name="state"></param>
  77. /// <returns></returns>
  78. IEnumerator Timing( float times, string state)
  79. {
  80. yield return new WaitForSeconds(0.1f);
  81. isWalk = false;
  82. yield return new WaitForSeconds(times-0.1f);
  83. CutState(state);
  84. }
  85. /// <summary>
  86. /// 切换状态
  87. /// </summary>
  88. /// <param name="state"></param>
  89. private void CutState(string state)
  90. {
  91. if(stateName!=null)
  92. mAnimator.SetBool(stateName, false);
  93. if (state == "Walk")
  94. isWalk = true;
  95. mAnimator.SetBool(state, true);
  96. stateName = state;
  97. }
  98. public void Walk(string posName)
  99. {
  100. switch (posName)
  101. {
  102. case "Gongye":
  103. if (robotState != "RobotOne")
  104. return;
  105. break;
  106. case "Subway":
  107. if (robotState != "RobotTwo")
  108. return;
  109. break;
  110. case "Chemical":
  111. if (robotState != "RobotThree")
  112. return;
  113. break;
  114. default:
  115. return;
  116. }
  117. transform.DOLocalRotate(new Vector3(0, -90, 0), 1f);
  118. StartCoroutine(Timing(1f, "Walk"));
  119. }
  120. }