WalkLerpPlayback.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System;
  6. public class WalkLerpPlayback : MonoBehaviour
  7. {
  8. // Waypoints and Rotations
  9. private Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] timePosRotList;
  10. // Animation
  11. private const string isWalking = "isWalking";
  12. // Private Journey Settings
  13. private int currentStartPoint;
  14. private float startTime;
  15. // Global GameObjects
  16. private GameObject[][] humansGO;
  17. private Animator[][] humansA;
  18. // Playback variables
  19. private int indexChangeRate;
  20. [HideInInspector]
  21. public bool rewind, pause, play; //PlaybackController
  22. private void Start()
  23. {
  24. // Get information from InstatiatePrefab
  25. humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  26. humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
  27. NavMeshAgent[][] humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
  28. // Read from CSV file and save time, position, rotation in matrix
  29. int index = gameObject.GetComponent<WriteInCSVNew>().index;
  30. string dir = Directory.GetCurrentDirectory();
  31. string reference = @"\Assets\Data_position\Walk" + index + ".csv";
  32. timePosRotList = gameObject.GetComponent<ReadFromCSVNew>().ReadFromCSVFile(dir + reference);
  33. // Set initial position and rotation
  34. currentStartPoint = 0;
  35. for (int i = 0; i < humansGO.Length; ++i)
  36. {
  37. for (int j = 0; j < humansGO[i].Length; ++j)
  38. {
  39. humansGO[i][j].transform.position = timePosRotList[0][0].Item2[0]; // First entry of Position
  40. humansGO[i][j].transform.rotation = timePosRotList[0][0].Item3[0]; // First entry of Rotation
  41. // Animation Idle
  42. humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
  43. humansNMA[i][j].enabled = false;
  44. }
  45. }
  46. SetTransform(currentStartPoint);
  47. currentStartPoint++;
  48. startTime = Time.time;
  49. }
  50. private void FixedUpdate()
  51. {
  52. if (pause)
  53. indexChangeRate = 0;
  54. if (play)
  55. indexChangeRate = 1;
  56. if (rewind)
  57. indexChangeRate = -1;
  58. if (play || rewind)
  59. {
  60. if ((currentStartPoint + indexChangeRate) < timePosRotList[0][0].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
  61. {
  62. currentStartPoint += indexChangeRate;
  63. SetTransform(currentStartPoint);
  64. }
  65. }
  66. else
  67. {
  68. for (int i = 0; i < humansGO.Length; ++i)
  69. {
  70. for (int j = 0; j < humansGO[i].Length; ++j)
  71. {
  72. humansA[i][j].SetBool("isWalking", false);
  73. }
  74. }
  75. }
  76. }
  77. private void SetTransform(int currentStartPoint)
  78. {
  79. for (int i = 0; i < humansGO.Length; ++i)
  80. {
  81. for (int j = 0; j < humansGO[i].Length; ++j)
  82. {
  83. // if so ArgumentOutOfRangeException
  84. if ((currentStartPoint + indexChangeRate) >= timePosRotList[i][j].Item2.Count || (currentStartPoint + indexChangeRate) < 0)
  85. {
  86. return;
  87. }
  88. Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
  89. Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + 1];
  90. Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
  91. Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + 1];
  92. float distCovered = (Time.time - startTime) * timePosRotList[i][j].Item4[currentStartPoint];
  93. float fracJourney = 1f;
  94. float journeyLength = Vector3.Distance(startPos, endPos);
  95. if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
  96. // Animation Idle, if startPoint == endPoint and startRot == endRot
  97. //if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count)
  98. //{
  99. // humansA[i][j].SetBool("isWalking", false);
  100. //}
  101. //else if (journeyLength > 0)
  102. //{
  103. // humansA[i][j].SetBool("isWalking", true);
  104. //}
  105. //humansA[i][j].speed = 2*(journeyLength / 0.2f);
  106. humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
  107. humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
  108. humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
  109. }
  110. }
  111. }
  112. }