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, List, List>[][] 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().humanGameObject; humansA = gameObject.GetComponent().humanAnimator; NavMeshAgent[][] humansNMA = gameObject.GetComponent().humanNavMeshAgent; stealHere = gameObject.GetComponent().stealHere; // Read from CSV file and save time, position, rotation in matrix int index = gameObject.GetComponent().index; string dir = Directory.GetCurrentDirectory(); string reference = @"\Assets\Data_position\Walk" + index + ".csv"; timePosRotList = gameObject.GetComponent().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; slider.maxValue = (maxTime / Time.deltaTime); 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 < 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) { 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; } }