123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.AI;
- using System.Collections.Generic;
- using System.IO;
- using System;
- [DefaultExecutionOrder(40)]
- [RequireComponent(typeof(InstantiatePrefab), typeof(WriteInCSV), typeof(ReadFromCSV))]
- public class WalkLerpPlayback : MonoBehaviour
- {
- // Waypoints and Rotations
- public Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] timePosRotList;
- // Animation
- private const string isWalking = "isWalking";
- private const string isGrabbing = "isGrabbing";
- // Private Journey Settings
- private int currentStartPoint;
- private float startTime;
- // Global GameObjects
- private GameObject[][] humansGO;
- private Animator[][] humansA;
- // Playback variables
- private int[] maxIJ;
- private int indexChangeRate;
- [HideInInspector]
- public bool rewind, pause, play; //PlaybackController
- // Slider Settings
- public Slider slider;
- private float prevSliderValue;
- // Thief Settings
- private Vector3 stealHere;
- private void Start()
- {
- // Get information from InstatiatePrefab
- humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
- humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
- NavMeshAgent[][] humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
- stealHere = gameObject.GetComponent<ControllingThief>().stealHere;
- // Read from CSV file and save time, position, rotation in matrix
- int index = gameObject.GetComponent<WriteInCSV>().index;
- string folderName = gameObject.GetComponent<WriteInCSV>().folderName;
- string dir = Directory.GetCurrentDirectory();
- string reference = @"\Assets\Data_position\" + folderName + @"\Walk" + index + @".csv";
- timePosRotList = gameObject.GetComponent<ReadFromCSV>().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;
- }
- }
- // Set the Slider settings
- if (slider == null)
- {
- Debug.Log("No Slider instance attached");
- return;
- }
- maxIJ = GetHumanWithLongestScreenTime();
- float maxTime = (float) Math.Round(timePosRotList[maxIJ[0]][maxIJ[1]].Item1[timePosRotList[maxIJ[0]][maxIJ[1]].Item1.Count - 1], 2);
- slider.minValue = 0;
- var tmpMaxValue = (maxTime / Time.deltaTime) + 1;
- if (slider.maxValue < tmpMaxValue)
- slider.maxValue = tmpMaxValue;
- slider.wholeNumbers = true;
- prevSliderValue = slider.value;
-
- SetTransform(currentStartPoint);
- currentStartPoint++;
- slider.value = 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[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
- {
- if(slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play
- {
- prevSliderValue = slider.value;
- currentStartPoint += indexChangeRate;
- slider.value = currentStartPoint;
- }
- else
- {
- prevSliderValue = slider.value;
- currentStartPoint = (int) slider.value;
- }
- SetTransform(currentStartPoint);
- }
- else if(slider.value - prevSliderValue != -1|| slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1)
- {
- currentStartPoint = (int) slider.value;
- }
- }
- else
- {
- // upper else-Statement only for pause
- if (slider.value - prevSliderValue != 0)
- {
- //prevSliderValue = slider.value;
- currentStartPoint = (int) slider.value;
- }
- for (int i = 0; i < humansGO.Length; ++i)
- {
- for (int j = 0; j < humansGO[i].Length; ++j)
- {
- if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
- {
- if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
- {
- humansGO[i][j].transform.position = timePosRotList[i][j].Item2[timePosRotList[i][j].Item2.Count - 1];
- humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[timePosRotList[i][j].Item3.Count - 1];
- }
- humansA[i][j].SetBool(isWalking, false);
- continue;
- }
- if (currentStartPoint < timePosRotList[i][j].Item2.Count)
- {
- humansGO[i][j].transform.position = timePosRotList[i][j].Item2[currentStartPoint]; // First entry of Position
- humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[currentStartPoint]; // First entry of Rotation
-
- humansA[i][j].SetBool(isWalking, false);
- humansA[i][j].SetBool(isGrabbing, 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 - 1) || (currentStartPoint + indexChangeRate) < 0)
- {
- if((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
- {
- humansGO[i][j].transform.position = timePosRotList[i][j].Item2[timePosRotList[i][j].Item2.Count - 1];
- humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[timePosRotList[i][j].Item3.Count - 1];
- }
- humansA[i][j].SetBool(isWalking, false);
- continue;
- }
- Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
- Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + indexChangeRate];
- Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
- Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + indexChangeRate];
- 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);
- if (Vector3.Distance(humansGO[i][j].transform.position, stealHere) <= 1.5f && timePosRotList[i][j].Item4[currentStartPoint] == 0.0f)
- humansA[i][j].SetBool(isGrabbing, true);
- else
- humansA[i][j].SetBool(isGrabbing, false);
- humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
- humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
- }
- }
- }
- private int[] GetHumanWithLongestScreenTime()
- {
- int[] maxIJ = new int[2] { 0, 0 };
- float currentMaxTime = 0;
- for (int i = 0; i < humansGO.Length; ++i)
- {
- for (int j = 0; j < humansGO[i].Length; ++j)
- {
- if(currentMaxTime < timePosRotList[i][j].Item1.Count)
- {
- currentMaxTime = timePosRotList[i][j].Item1.Count;
- maxIJ[0] = i;
- maxIJ[1] = j;
- }
- }
- }
-
- return maxIJ;
- }
- }
|