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 posList; private List 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().index; string dir = Directory.GetCurrentDirectory(); string reference = @"\Assets\Data_position\Walk" + index + ".csv"; // var -> Tuple, List> var posRotList = gameObject.GetComponent().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(); 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(); } } }