WalkPos_LerpNew.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System;
  6. public class WalkPos_LerpNew : MonoBehaviour
  7. {
  8. // Waypoints and Rotations
  9. private Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] 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. //Update possible
  18. private bool doUpdate = false;
  19. private void Start()
  20. {
  21. // Get information from InstatiatePrefab
  22. humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  23. humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
  24. humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
  25. // Read from CSV file and save time, position, rotation in matrix
  26. int index = gameObject.GetComponent<WriteInCSV>().index;
  27. string folderName = gameObject.GetComponent<WriteInCSV>().folderName;
  28. string dir = Directory.GetCurrentDirectory();
  29. string reference = @"\Assets\Data_position\"+ folderName + @"\Walk" + index + @".csv";
  30. timePosRotList = gameObject.GetComponent<ReadFromCSV>().ReadFromCSVFile(dir + reference);
  31. // Set initial position and rotation
  32. currentStartPoint = new int[humansGO.Length][];
  33. for (int i = 0; i < humansGO.Length; ++i)
  34. {
  35. currentStartPoint[i] = new int[humansGO[i].Length];
  36. for (int j = 0; j < humansGO[i].Length; ++j)
  37. {
  38. currentStartPoint[i][j] = 0;
  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", false);
  43. humansNMA[i][j].enabled = false;
  44. }
  45. }
  46. startTime = Time.time;
  47. // All required parameters for Update have been set
  48. doUpdate = true;
  49. }
  50. void FixedUpdate()
  51. {
  52. if (!doUpdate) return;
  53. for(int i = 0; i < humansGO.Length; ++i)
  54. {
  55. for(int j = 0; j < humansGO[i].Length; ++j)
  56. {
  57. Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint[i][j]];
  58. Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint[i][j] + 1];
  59. Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint[i][j]];
  60. Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint[i][j] + 1];
  61. float distCovered = (Time.time - startTime) * humansA[i][j].speed;
  62. float fracJourney = 1f;
  63. float journeyLength = Vector3.Distance(startPos, endPos);
  64. if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
  65. // Animation Idle, if startPoint == endPoint and startRot == endRot
  66. if (journeyLength <= 0 && startRot.Equals(endRot))
  67. {
  68. humansA[i][j].SetBool("isWalking", false);
  69. }
  70. else if (journeyLength > 0)
  71. {
  72. humansA[i][j].SetBool("isWalking", true);
  73. }
  74. humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
  75. humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
  76. if (fracJourney >= 1f &&
  77. currentStartPoint[i][j] + 2 < timePosRotList[i][j].Item2.Count &&
  78. currentStartPoint[i][j] + 2 < timePosRotList[i][j].Item3.Count)
  79. {
  80. currentStartPoint[i][j]++;
  81. }
  82. }
  83. }
  84. }
  85. }