|
@@ -0,0 +1,82 @@
|
|
|
+using UnityEngine.AI;
|
|
|
+using UnityEngine;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System;
|
|
|
+
|
|
|
+public class WalkPos_NavMeshAgent : MonoBehaviour
|
|
|
+{
|
|
|
+ public int fileIndex = 1;
|
|
|
+ private string path;
|
|
|
+ private List<Vector3> vec = new List<Vector3>();
|
|
|
+
|
|
|
+
|
|
|
+ public float wanderRadius;
|
|
|
+ public float wanderTimer;
|
|
|
+
|
|
|
+ private Animator anim;
|
|
|
+ private NavMeshAgent agent;
|
|
|
+ private Transform target;
|
|
|
+ private float timer;
|
|
|
+ private Vector3 newPos;
|
|
|
+
|
|
|
+ // Use this for initialization
|
|
|
+ void Start()
|
|
|
+ {
|
|
|
+ // set the path to the needed index
|
|
|
+ string currentPath = Directory.GetCurrentDirectory();
|
|
|
+ string reference = @"\Assets\CSV_files\Walk" + fileIndex + @".txt";
|
|
|
+ path = currentPath + reference;
|
|
|
+ // save all positions in "vec"
|
|
|
+ ReadPosFromFile readTxt = new ReadPosFromFile();
|
|
|
+ vec = readTxt.ReadFromTxtFile(path);
|
|
|
+
|
|
|
+ // throw if "vec" is empty
|
|
|
+ if(vec == null)
|
|
|
+ {
|
|
|
+ throw new ArgumentNullException("File does not exist or File is empty!!", nameof(vec));
|
|
|
+ }
|
|
|
+
|
|
|
+ agent = this.GetComponent<NavMeshAgent>();
|
|
|
+ anim = this.GetComponent<Animator>();
|
|
|
+ anim.SetBool("isWalking", true);
|
|
|
+ anim.SetBool("isIdle", false);
|
|
|
+ newPos = vec[0];
|
|
|
+ timer = wanderTimer;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Update is called once per frame
|
|
|
+ void Update()
|
|
|
+ {
|
|
|
+ timer += Time.deltaTime;
|
|
|
+
|
|
|
+ if (timer >= wanderTimer)
|
|
|
+ {
|
|
|
+ // generating random destination position
|
|
|
+ //newPos = RandomNavSphere(transform.position, wanderRadius, -1);
|
|
|
+ newPos.y = -3f;
|
|
|
+ if (agent.isActiveAndEnabled)
|
|
|
+ agent.SetDestination(newPos);
|
|
|
+ anim.SetBool("isWalking", true);
|
|
|
+ anim.SetBool("isIdle", false);
|
|
|
+ timer = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // if x and z coordinates of newPos are the same as the position, humans reached destination and idle animation can start
|
|
|
+ if (newPos.x == transform.position.x && newPos.z == transform.position.z)
|
|
|
+ {
|
|
|
+ anim.SetBool("isWalking", false);
|
|
|
+ anim.SetBool("isIdle", true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public Vector3 FromFileNavSphere()
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ return vec[0];
|
|
|
+ }
|
|
|
+}
|