Robot.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. isWalk = false;
  35. switch (other.name)
  36. {
  37. case "RobotOne":
  38. transform.DOLocalRotate(new Vector3(0, 90, 0), 1f);
  39. StartCoroutine(Timing(1.5f, "Point_left"));
  40. break;
  41. case "RobotTwo":
  42. transform.DOLocalRotate(new Vector3(0, 90, 0), 1f);
  43. StartCoroutine(Timing(1.5f, "Point_right"));
  44. break;
  45. case "RobotThree":
  46. transform.DOLocalRotate(new Vector3(0, 90, 0), 1f);
  47. StartCoroutine(Timing(1.5f, "Point_left"));
  48. break;
  49. case "RobotFour":
  50. transform.DOLocalRotate(new Vector3(0, 90, 0), 1f);
  51. StartCoroutine(Timing(1.5f, "Point_left"));
  52. break;
  53. default:
  54. break;
  55. }
  56. robotState = other.name;
  57. }
  58. }
  59. private void StateOne()
  60. {
  61. CutState("Waving");
  62. StartCoroutine(Timing(1.5f, "Point_left"));
  63. StartCoroutine(StateTwo(4f));
  64. }
  65. IEnumerator StateTwo(float times)
  66. {
  67. yield return new WaitForSeconds(times);
  68. transform.DOLocalRotate(new Vector3(0, -90, 0), 1f);
  69. StartCoroutine(Timing(1f, "Walk"));
  70. // 转身
  71. // 行走, 进入下一个待定位置
  72. }
  73. /// <summary>
  74. /// 定时切换状态
  75. /// </summary>
  76. /// <param name="times"></param>
  77. /// <param name="state"></param>
  78. /// <returns></returns>
  79. IEnumerator Timing( float times, string state)
  80. {
  81. yield return new WaitForSeconds(times);
  82. CutState(state);
  83. }
  84. /// <summary>
  85. /// 切换状态
  86. /// </summary>
  87. /// <param name="state"></param>
  88. private void CutState(string state)
  89. {
  90. if(stateName!=null)
  91. mAnimator.SetBool(stateName, false);
  92. if (state == "Walk")
  93. isWalk = true;
  94. mAnimator.SetBool(state, true);
  95. stateName = state;
  96. }
  97. public void Walk(string posName)
  98. {
  99. switch (posName)
  100. {
  101. case "Gongye":
  102. if (robotState != "RobotOne")
  103. return;
  104. break;
  105. case "Subway":
  106. if (robotState != "RobotTwo")
  107. return;
  108. break;
  109. case "QuestionContral":
  110. if (robotState != "RobotThree")
  111. return;
  112. break;
  113. default:
  114. return;
  115. }
  116. transform.DOLocalRotate(new Vector3(0, -90, 0), 1f);
  117. StartCoroutine(Timing(1f, "Walk"));
  118. }
  119. }