123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using UnityEngine;
- using UnityEngine.AI;
- using System.Collections.Generic;
- using System.IO;
- using System;
- public class WalkLerpPlayback : MonoBehaviour
- {
- // Waypoints and Rotations
- private Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] timePosRotList;
- // Animation
- private const string isWalking = "isWalking";
- // Private Journey Settings
- private int currentStartPoint;
- private float startTime;
- // Global GameObjects
- private GameObject[][] humansGO;
- private Animator[][] humansA;
- // Playback variables
- private int indexChangeRate;
- [HideInInspector]
- public bool rewind, pause, play; //PlaybackController
- private void Start()
- {
- // Get information from InstatiatePrefab
- humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
- humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
- NavMeshAgent[][] humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
- // Read from CSV file and save time, position, rotation in matrix
- int index = gameObject.GetComponent<WriteInCSVNew>().index;
- string dir = Directory.GetCurrentDirectory();
- string reference = @"\Assets\Data_position\Walk" + index + ".csv";
- timePosRotList = gameObject.GetComponent<ReadFromCSVNew>().ReadFromCSVFile(dir + reference);
- // Set initial position and rotation
- currentStartPoint = 0;
- for (int i = 0; i < humansGO.Length; ++i)
- {
- for (int j = 0; j < humansGO[i].Length; ++j)
- {
- humansGO[i][j].transform.position = timePosRotList[0][0].Item2[0]; // First entry of Position
- humansGO[i][j].transform.rotation = timePosRotList[0][0].Item3[0]; // First entry of Rotation
- // Animation Idle
- humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
- humansNMA[i][j].enabled = false;
- }
- }
- SetTransform(currentStartPoint);
- currentStartPoint++;
- startTime = Time.time;
- }
- private void FixedUpdate()
- {
- if (pause)
- indexChangeRate = 0;
- if (play)
- indexChangeRate = 1;
- if (rewind)
- indexChangeRate = -1;
-
- if (play || rewind)
- {
- if ((currentStartPoint + indexChangeRate) < timePosRotList[0][0].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
- {
- currentStartPoint += indexChangeRate;
- SetTransform(currentStartPoint);
- }
- }
- else
- {
- for (int i = 0; i < humansGO.Length; ++i)
- {
- for (int j = 0; j < humansGO[i].Length; ++j)
- {
- humansA[i][j].SetBool("isWalking", false);
- }
- }
- }
- }
- private void SetTransform(int currentStartPoint)
- {
- for (int i = 0; i < humansGO.Length; ++i)
- {
- for (int j = 0; j < humansGO[i].Length; ++j)
- {
- // if so ArgumentOutOfRangeException
- if ((currentStartPoint + indexChangeRate) >= timePosRotList[i][j].Item2.Count || (currentStartPoint + indexChangeRate) < 0)
- {
- return;
- }
- Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
- Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + 1];
- Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
- Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + 1];
- float distCovered = (Time.time - startTime) * timePosRotList[i][j].Item4[currentStartPoint];
- float fracJourney = 1f;
- float journeyLength = Vector3.Distance(startPos, endPos);
- if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
- // Animation Idle, if startPoint == endPoint and startRot == endRot
- //if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count)
- //{
- // humansA[i][j].SetBool("isWalking", false);
- //}
- //else if (journeyLength > 0)
- //{
- // humansA[i][j].SetBool("isWalking", true);
- //}
- //humansA[i][j].speed = 2*(journeyLength / 0.2f);
- humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
- humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
- humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
- }
- }
- }
- }
|