WalkLerpPlayback.cs 8.1 KB

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