WalkPos_LerpOld.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. public class WalkPos_LerpOld : MonoBehaviour
  5. {
  6. // Public Settings
  7. public float speed = 1.0f;
  8. // Waypoints and Rotations
  9. private Vector3 startPoint, endPoint;
  10. private Quaternion startRot, endRot;
  11. private List<Vector3> posList;
  12. private List<Quaternion> rotList;
  13. // Private Journey Settings
  14. private int currentStartPoint;
  15. private float startTime;
  16. private float journeyLength;
  17. //Animator
  18. private Animator anim;
  19. //Update possible
  20. private bool doUpdate = false;
  21. private void Start()
  22. {
  23. int index = gameObject.GetComponent<WriteInCSVOld>().index;
  24. string dir = Directory.GetCurrentDirectory();
  25. string reference = @"\Assets\Data_position\Walk" + index + ".csv";
  26. // var -> Tuple<List<Vector3>, List<Quaternion>>
  27. var posRotList = gameObject.GetComponent<ReadFromCSVOld>().ReadFromCSVFile(dir + reference);
  28. posList = posRotList.Item1;
  29. rotList = posRotList.Item2;
  30. currentStartPoint = 0;
  31. transform.position = posList[0];
  32. transform.rotation = rotList[0];
  33. SetPoints();
  34. // Animation Walking
  35. anim = this.GetComponent<Animator>();
  36. anim.SetBool("isWalking", true);
  37. anim.SetBool("isIdle", false);
  38. // All required parameters for Update have been set
  39. doUpdate = true;
  40. }
  41. void SetPoints()
  42. {
  43. startPoint = posList[currentStartPoint];
  44. endPoint = posList[currentStartPoint + 1];
  45. startRot = rotList[currentStartPoint];
  46. endRot = rotList[currentStartPoint + 1];
  47. startTime = Time.time;
  48. journeyLength = Vector3.Distance(startPoint, endPoint);
  49. }
  50. // Update is called once per frame
  51. void FixedUpdate()
  52. {
  53. if (!doUpdate) return;
  54. float distCovered = (Time.time - startTime) * speed;
  55. float fracJourney = 1f;
  56. if (journeyLength != 0) fracJourney = distCovered / journeyLength;
  57. // Animation Idle, if startPoint == endPoint and startRot == endRot
  58. if (journeyLength == 0 && startRot.Equals(endRot))
  59. {
  60. anim.SetBool("isWalking", false);
  61. anim.SetBool("isIdle", true);
  62. }
  63. else if(!anim.GetBool("isWalking"))
  64. {
  65. anim.SetBool("isWalking", true);
  66. anim.SetBool("isIdle", false);
  67. }
  68. transform.position = Vector3.Lerp(startPoint, endPoint, fracJourney);
  69. transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
  70. if (fracJourney >= 1f && currentStartPoint + 2 < posList.Count && currentStartPoint + 2 < rotList.Count)
  71. {
  72. currentStartPoint++;
  73. SetPoints();
  74. }
  75. }
  76. }