1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using UnityEngine;
- using System.Collections.Generic;
- using System.IO;
- public class WalkPos_Lerp : 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;
- private void Start()
- {
- int index = gameObject.GetComponent<WriteInFile>().index;
- string dir = Directory.GetCurrentDirectory();
- string reference = @"\Assets\CSV_files\Walk" + index + ".txt";
- // var -> Tuple<List<Vector3>, List<Quaternion>>
- var posRotList = gameObject.GetComponent<ReadFromFile>().ReadFromTxtFile(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);
- }
- 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 Update()
- {
- 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 == 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();
- }
- }
- }
|