WalkLerpPlayback.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.AI;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System;
  7. [DefaultExecutionOrder(40)]
  8. [RequireComponent(typeof(InstantiatePrefab), typeof(WriteInCSV), typeof(ReadFromCSV))]
  9. public class WalkLerpPlayback : MonoBehaviour
  10. {
  11. // Waypoints and Rotations
  12. private Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] timePosRotList;
  13. // Animation
  14. private const string isWalking = "isWalking";
  15. // Private Journey Settings
  16. private int currentStartPoint;
  17. private float startTime;
  18. // Global GameObjects
  19. private GameObject[][] humansGO;
  20. private Animator[][] humansA;
  21. // Playback variables
  22. private int[] maxIJ;
  23. private int indexChangeRate;
  24. [HideInInspector]
  25. public bool rewind, pause, play; //PlaybackController
  26. // Slider Settings
  27. public Slider slider;
  28. private float prevSliderValue;
  29. private void Start()
  30. {
  31. // Get information from InstatiatePrefab
  32. humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  33. humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
  34. NavMeshAgent[][] humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
  35. // Read from CSV file and save time, position, rotation in matrix
  36. int index = gameObject.GetComponent<WriteInCSV>().index;
  37. string dir = Directory.GetCurrentDirectory();
  38. string reference = @"\Assets\Data_position\Walk" + index + ".csv";
  39. timePosRotList = gameObject.GetComponent<ReadFromCSV>().ReadFromCSVFile(dir + reference);
  40. // Set initial position and rotation
  41. currentStartPoint = 0;
  42. for (int i = 0; i < humansGO.Length; ++i)
  43. {
  44. for (int j = 0; j < humansGO[i].Length; ++j)
  45. {
  46. humansGO[i][j].transform.position = timePosRotList[0][0].Item2[0]; // First entry of Position
  47. humansGO[i][j].transform.rotation = timePosRotList[0][0].Item3[0]; // First entry of Rotation
  48. // Animation Idle
  49. humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
  50. humansNMA[i][j].enabled = false;
  51. }
  52. }
  53. // Set the Slider settings
  54. if (slider == null)
  55. {
  56. Debug.Log("No Slider instance attached");
  57. return;
  58. }
  59. maxIJ = GetHumanWithLongestScreenTime();
  60. float maxTime = (float) Math.Round(timePosRotList[maxIJ[0]][maxIJ[1]].Item1[timePosRotList[maxIJ[0]][maxIJ[1]].Item1.Count - 1], 2);
  61. slider.minValue = 0;
  62. slider.maxValue = (maxTime / Time.deltaTime);
  63. slider.wholeNumbers = true;
  64. prevSliderValue = slider.value;
  65. SetTransform(currentStartPoint);
  66. currentStartPoint++;
  67. slider.value = currentStartPoint;
  68. startTime = Time.time;
  69. }
  70. private void FixedUpdate()
  71. {
  72. if (pause)
  73. indexChangeRate = 0;
  74. if (play)
  75. indexChangeRate = 1;
  76. if (rewind)
  77. indexChangeRate = -1;
  78. if (play || rewind)
  79. {
  80. if ((currentStartPoint + indexChangeRate) < timePosRotList[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
  81. {
  82. if(slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play
  83. {
  84. prevSliderValue = slider.value;
  85. currentStartPoint += indexChangeRate;
  86. slider.value = currentStartPoint;
  87. }
  88. else
  89. {
  90. prevSliderValue = slider.value;
  91. currentStartPoint = (int) slider.value;
  92. }
  93. SetTransform(currentStartPoint);
  94. }
  95. else if(slider.value - prevSliderValue != -1|| slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1)
  96. {
  97. currentStartPoint = (int) slider.value;
  98. }
  99. }
  100. else
  101. {
  102. // upper else-Statement only for pause
  103. if (slider.value - prevSliderValue != 0)
  104. {
  105. //prevSliderValue = slider.value;
  106. currentStartPoint = (int) slider.value;
  107. }
  108. for (int i = 0; i < humansGO.Length; ++i)
  109. {
  110. for (int j = 0; j < humansGO[i].Length; ++j)
  111. {
  112. humansGO[i][j].transform.position = timePosRotList[i][j].Item2[currentStartPoint]; // First entry of Position
  113. humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[currentStartPoint]; // First entry of Rotation
  114. humansA[i][j].SetBool("isWalking", false);
  115. }
  116. }
  117. }
  118. }
  119. private void SetTransform(int currentStartPoint)
  120. {
  121. for (int i = 0; i < humansGO.Length; ++i)
  122. {
  123. for (int j = 0; j < humansGO[i].Length; ++j)
  124. {
  125. // if so ArgumentOutOfRangeException
  126. if ((currentStartPoint + indexChangeRate) >= timePosRotList[i][j].Item2.Count || (currentStartPoint + indexChangeRate) < 0)
  127. {
  128. return;
  129. }
  130. Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
  131. Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + 1];
  132. Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
  133. Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + 1];
  134. float distCovered = (Time.time - startTime) * timePosRotList[i][j].Item4[currentStartPoint];
  135. float fracJourney = 1f;
  136. float journeyLength = Vector3.Distance(startPos, endPos);
  137. if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
  138. // Animation Idle, if startPoint == endPoint and startRot == endRot
  139. //if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count)
  140. //{
  141. // humansA[i][j].SetBool("isWalking", false);
  142. //}
  143. //else if (journeyLength > 0)
  144. //{
  145. // humansA[i][j].SetBool("isWalking", true);
  146. //}
  147. //humansA[i][j].speed = 2*(journeyLength / 0.2f);
  148. humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
  149. humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
  150. humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
  151. }
  152. }
  153. }
  154. private int[] GetHumanWithLongestScreenTime()
  155. {
  156. int[] maxIJ = new int[2] { 0, 0 };
  157. float currentMaxTime = 0;
  158. for (int i = 0; i < humansGO.Length; ++i)
  159. {
  160. for (int j = 0; j < humansGO[i].Length; ++j)
  161. {
  162. if(currentMaxTime < timePosRotList[i][j].Item1.Count)
  163. {
  164. currentMaxTime = timePosRotList[i][j].Item1.Count;
  165. maxIJ[0] = i;
  166. maxIJ[1] = j;
  167. }
  168. }
  169. }
  170. return maxIJ;
  171. }
  172. }