123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using UnityEngine;
- using System.Collections.Generic;
- using System.IO;
- public class WalkPos_LerpOld : MonoBehaviour
- {
- // Public Settings
- public float speed = 1.0f;
- // Waypoints and Rotations
- private Vector3 startPoint, endPoint;
- private Quaternion startRot, endRot;
- private List<Vector3> posList;
- private List<Quaternion> rotList;
-
- // Private Journey Settings
- private int currentStartPoint;
- private float startTime;
- private float journeyLength;
- //Animator
- private Animator anim;
- //Update possible
- private bool doUpdate = false;
- private void Start()
- {
- int index = gameObject.GetComponent<WriteInCSVOld>().index;
- string dir = Directory.GetCurrentDirectory();
- string reference = @"\Assets\Data_position\Walk" + index + ".csv";
- // var -> Tuple<List<Vector3>, List<Quaternion>>
- var posRotList = gameObject.GetComponent<ReadFromCSVOld>().ReadFromCSVFile(dir + reference);
- posList = posRotList.Item1;
- rotList = posRotList.Item2;
-
- currentStartPoint = 0;
- transform.position = posList[0];
- transform.rotation = rotList[0];
- SetPoints();
- // Animation Walking
- anim = this.GetComponent<Animator>();
- anim.SetBool("isWalking", true);
- anim.SetBool("isIdle", false);
- // All required parameters for Update have been set
- doUpdate = true;
- }
- void SetPoints()
- {
- startPoint = posList[currentStartPoint];
- endPoint = posList[currentStartPoint + 1];
- startRot = rotList[currentStartPoint];
- endRot = rotList[currentStartPoint + 1];
- startTime = Time.time;
- journeyLength = Vector3.Distance(startPoint, endPoint);
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- if (!doUpdate) return;
- float distCovered = (Time.time - startTime) * speed;
- float fracJourney = 1f;
- if (journeyLength != 0) fracJourney = distCovered / journeyLength;
- // Animation Idle, if startPoint == endPoint and startRot == endRot
- if (journeyLength == 0 && startRot.Equals(endRot))
- {
- anim.SetBool("isWalking", false);
- anim.SetBool("isIdle", true);
- }
- else if(!anim.GetBool("isWalking"))
- {
- anim.SetBool("isWalking", true);
- anim.SetBool("isIdle", false);
- }
- transform.position = Vector3.Lerp(startPoint, endPoint, fracJourney);
- transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
- if (fracJourney >= 1f && currentStartPoint + 2 < posList.Count && currentStartPoint + 2 < rotList.Count)
- {
- currentStartPoint++;
- SetPoints();
- }
- }
- }
|