WalkLerpPlayback.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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>>[][] timePosRotList;
  10. // Private Journey Settings
  11. private int currentStartPoint;
  12. private float startTime;
  13. //Animator
  14. private GameObject[][] humansGO;
  15. private Animator[][] humansA;
  16. private NavMeshAgent[][] humansNMA;
  17. private void Start()
  18. {
  19. // Get information from InstatiatePrefab
  20. humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  21. humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
  22. humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
  23. // Read from CSV file and save time, position, rotation in matrix
  24. int index = gameObject.GetComponent<WriteInCSVNew>().index;
  25. string dir = Directory.GetCurrentDirectory();
  26. string reference = @"\Assets\CSV_files\Walk" + index + ".csv";
  27. timePosRotList = gameObject.GetComponent<ReadFromCSVNew>().ReadFromCSVFile(dir + reference);
  28. // Set initial position and rotation
  29. currentStartPoint = 0;
  30. for (int i = 0; i < humansGO.Length; ++i)
  31. {
  32. for (int j = 0; j < humansGO[i].Length; ++j)
  33. {
  34. humansGO[i][j].transform.position = timePosRotList[0][0].Item2[0]; // First entry of Position
  35. humansGO[i][j].transform.rotation = timePosRotList[0][0].Item3[0]; // First entry of Rotation
  36. // Animation Idle
  37. humansA[i][j].SetBool("isWalking", false);
  38. humansA[i][j].SetBool("isIdle", true);
  39. humansNMA[i][j].enabled = false;
  40. }
  41. }
  42. SetTransform(currentStartPoint);
  43. currentStartPoint++;
  44. startTime = Time.time;
  45. }
  46. private int indexChangeRate;
  47. //PlaybackController is setting the following variables
  48. [HideInInspector]
  49. public bool rewind, pause, play;
  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. humansA[i][j].SetBool("isIdle", true);
  74. }
  75. }
  76. }
  77. }
  78. private void SetTransform(int currentStartPoint)
  79. {
  80. for (int i = 0; i < humansGO.Length; ++i)
  81. {
  82. for (int j = 0; j < humansGO[i].Length; ++j)
  83. {
  84. // if so ArgumentOutOfRangeException
  85. if ((currentStartPoint + indexChangeRate) >= timePosRotList[i][j].Item2.Count || (currentStartPoint + indexChangeRate) < 0)
  86. {
  87. return;
  88. }
  89. Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
  90. Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + 1];
  91. Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
  92. Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + 1];
  93. float distCovered = (Time.time - startTime) * humansA[i][j].speed;
  94. float fracJourney = 1f;
  95. float journeyLength = Vector3.Distance(startPos, endPos);
  96. if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
  97. // Animation Idle, if startPoint == endPoint and startRot == endRot
  98. if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count)
  99. {
  100. humansA[i][j].SetBool("isWalking", false);
  101. humansA[i][j].SetBool("isIdle", true);
  102. }
  103. else if (journeyLength > 0)
  104. {
  105. humansA[i][j].SetBool("isWalking", true);
  106. humansA[i][j].SetBool("isIdle", false);
  107. }
  108. //humansA[i][j].speed = 2*(journeyLength / 0.2f);
  109. humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
  110. humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
  111. }
  112. }
  113. }
  114. }