AutoMove.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. using UnityEngine.XR.ARFoundation;
  6. using TMPro;
  7. //using UnityEngine.UIElements;
  8. //using UnityEditor.PackageManager.Requests;
  9. public class AutoMove : MonoBehaviour
  10. {
  11. public Vector3[] points;
  12. private ARSessionOrigin sessionOrigin;
  13. private int destPoint = 0;
  14. private NavMeshAgent agent;
  15. private Vector3 randomCoordinate;
  16. private bool isWait = true; //小狗是否在等待
  17. private bool isActive = false; //小狗是否可以行动
  18. private bool isBack = false; //小狗是否可以回来
  19. private Animator animator;
  20. private Text txt2;
  21. private Text txt;
  22. private Button myButton;
  23. private Button sittingBtn;
  24. private Button angryBtn;
  25. private float x = 0;
  26. private float z = 0;
  27. public string horizontalAxis = "Horizontal"; // 在输入管理器中设置的水平轴名称
  28. public string verticalAxis = "Vertical"; // 在输入管理器中设置的垂直轴名称
  29. public Vector3 worldPosition = new Vector3(0f, 0f, 0f);
  30. private int randomNumber = 0;
  31. private int idleTime = 4; //待机时间
  32. public int 体力值 = 35;
  33. private float lastX;
  34. private float posX;
  35. public GameObject ARcamera;
  36. void Start()
  37. {
  38. //Debug.LogError("这个一条信息");
  39. sessionOrigin = FindObjectOfType<ARSessionOrigin>();
  40. agent = GetComponent<NavMeshAgent>();
  41. animator = GetComponent<Animator>();
  42. // 禁用自动制动将允许点之间的
  43. // 连续移动(即,代理在接近目标点时
  44. // 不会减速)。
  45. agent.autoBraking = false;
  46. //txt = GameObject.Find("txt").GetComponent<Text>();
  47. //txt2 = GameObject.Find("txt2").GetComponent<Text>();
  48. x = this.transform.position.x;
  49. z = this.transform.position.z;
  50. //myButton = GameObject.Find("backBtn").GetComponent<Button>();
  51. //myButton.onClick.AddListener(OnButtonClick);
  52. //sittingBtn = GameObject.Find("sittingBtn").GetComponent<Button>();
  53. //sittingBtn.onClick.AddListener(OnSittingClick);
  54. //angryBtn = GameObject.Find("angryBtn").GetComponent<Button>();
  55. //angryBtn.onClick.AddListener(OnAngryBtnClick);
  56. GotoNextPoint();
  57. }
  58. void OnSittingClick()
  59. {
  60. animator.SetBool("isRun", false);
  61. animator.SetInteger("state", 3);
  62. }
  63. void OnAngryBtnClick()
  64. {
  65. animator.SetBool("isRun", false);
  66. animator.SetInteger("state", 1);
  67. }
  68. void OnButtonClick()
  69. {
  70. if(myButton.GetComponentInChildren<Text>().text == "小狗回来") {
  71. x = worldPosition.x;
  72. z = worldPosition.z;
  73. animator.SetBool("isRun", true);
  74. randomCoordinate = new Vector3(0, -1.1f, 1.2f);
  75. //txt.text = randomCoordinate.ToString("F2");
  76. agent.destination = randomCoordinate;
  77. isBack = true;
  78. isWait = false;
  79. // 修改按钮文本
  80. myButton.GetComponentInChildren<Text>().text = "自己玩耍";
  81. } else
  82. {
  83. isActive = false;
  84. isWait = true;
  85. myButton.GetComponentInChildren<Text>().text = "小狗回来";
  86. }
  87. }
  88. void GotoNextPoint()
  89. {
  90. // 如果未设置任何点,则返回
  91. if (points.Length == 0)
  92. return;
  93. //将代理设置为前往当前选定的目标。
  94. agent.destination = points[destPoint];
  95. //选择数组中的下一个点作为目标,
  96. // 如有必要,循环到开始。
  97. destPoint = (destPoint + 1) % points.Length;
  98. }
  99. void followPosition()
  100. {
  101. StartCoroutine(DelayedFunction());
  102. }
  103. IEnumerator DelayedFunction1()
  104. {
  105. yield return new WaitForSeconds(5);
  106. followPosition();
  107. }
  108. IEnumerator DelayedFunction()
  109. {
  110. isWait = false;
  111. if (体力值 >= 60)
  112. {
  113. animator.SetBool("isrun", true);
  114. agent.speed = 1f;
  115. animator.SetInteger("state", 1);
  116. //randomCoordinate = new Vector3(Random.Range(x - 4, x + 4), -1.5f, Random.Range(z - 4, z + 4));
  117. randomCoordinate = new Vector3(Random.Range(540f, 554f), 0f, Random.Range(540f, 554f));
  118. //Debug.Log("Generated random coordinate: " + randomCoordinate);
  119. agent.destination = randomCoordinate;
  120. yield return new WaitForSeconds(1);
  121. } else if (体力值 >= 30)
  122. {
  123. animator.SetBool("isrun", true);
  124. agent.speed = 0.5f;
  125. animator.SetInteger("state", 2);
  126. animator.SetBool("isrun", true);
  127. //randomCoordinate = new Vector3(Random.Range(x - 4, x + 4), -1.5f, Random.Range(z - 4, z + 4));
  128. randomCoordinate = new Vector3(Random.Range(540f, 554f), 0f, Random.Range(540f, 554f));
  129. Debug.Log("Generated random coordinate: " + randomCoordinate);
  130. agent.destination = randomCoordinate;
  131. yield return new WaitForSeconds(1);
  132. } else
  133. {
  134. animator.SetBool("isrun", false);
  135. animator.SetBool("isIdle", true);
  136. animator.SetInteger("idle", Random.Range(0, 6));
  137. yield return new WaitForSeconds(5);
  138. }
  139. isWait = true;
  140. }
  141. // 定义协程函数
  142. IEnumerator DelayedFunction3()
  143. {
  144. // 延迟5秒
  145. agent.speed = 0.5f;
  146. isWait = false;
  147. //animator.SetBool("isRun", false);
  148. randomNumber = Random.Range(1, 3);
  149. animator.SetInteger("state", randomNumber);
  150. if(randomNumber == 0)
  151. {
  152. animator.SetBool("isIdle", true);
  153. animator.SetInteger("idle", Random.Range(0, 6));
  154. yield return new WaitForSeconds(1);
  155. } else
  156. {
  157. //randomCoordinate = new Vector3(Random.Range(x - 4, x + 4), -1.5f, Random.Range(z - 4, z + 4));
  158. randomCoordinate = new Vector3(Random.Range(540f, 554f), 0f, Random.Range(540f, 554f));
  159. //Debug.Log("Generated random coordinate: " + randomCoordinate);
  160. //Debug.Log(randomCoordinate);
  161. agent.destination = randomCoordinate;
  162. isWait = false;
  163. yield return new WaitForSeconds(1);
  164. }
  165. if(randomNumber == 1)
  166. {
  167. agent.speed = 1;
  168. }
  169. //float waitTime = Random.Range(idleTime / 2, idleTime * 2);
  170. if (!isBack)
  171. {
  172. if(randomNumber != 0)
  173. {
  174. ////randomCoordinate = new Vector3(Random.Range(x - 4, x + 4), -1.5f, Random.Range(z - 4, z + 4));
  175. //randomCoordinate = new Vector3(Random.Range(x - 8, x + 8), 0f, Random.Range(z - 8, z + 8));
  176. ////Debug.Log("Generated random coordinate: " + randomCoordinate);
  177. //agent.destination = randomCoordinate;
  178. }
  179. isWait = true;
  180. //animator.SetBool("isRun", true);
  181. animator.SetBool("isIdle", false);
  182. }
  183. }
  184. void Update()
  185. {
  186. //myText.text = this.transform.position.ToString("F2");
  187. //txt.text = z.ToString("F2");
  188. //当代理接近当前目标点时,
  189. // 选择下一个目标点。
  190. //txt.text = isWait.ToString();
  191. //txt2.text = isBack.ToString();
  192. if (体力值 >= 30)
  193. {
  194. animator.SetBool("isrun", true);
  195. animator.SetBool("isIdle", false);
  196. agent.speed = 0.5f;
  197. if (体力值 >= 60)
  198. {
  199. agent.speed = 1f;
  200. }
  201. if (!agent.pathPending && agent.remainingDistance < 0.05f && !isBack)
  202. {
  203. if (isWait)
  204. {
  205. animator.SetInteger("state", 0);
  206. //animator.SetBool("isIdle", true);
  207. //animator.SetInteger("idle", Random.Range(0, 6));
  208. StartCoroutine(DelayedFunction());
  209. //followPosition();
  210. }
  211. }
  212. if (!agent.pathPending && agent.remainingDistance < 0.05f && isBack)
  213. {
  214. randomCoordinate = new Vector3(0, -1.1f, 1f);
  215. agent.destination = randomCoordinate;
  216. isBack = false;
  217. isActive = true;
  218. }
  219. if (!agent.pathPending && agent.remainingDistance < 0.05f && !isBack)
  220. {
  221. if (isActive)
  222. {
  223. animator.SetBool("isrun", false);
  224. animator.SetInteger("state", 0);
  225. isActive = false;
  226. }
  227. }
  228. } else
  229. {
  230. agent.speed = 0f;
  231. animator.SetInteger("state", 0);
  232. animator.SetBool("isrun", false);
  233. StartCoroutine(DelayedFunction());
  234. }
  235. // 获取水平和垂直轴的值
  236. float h = Input.GetAxis(horizontalAxis);
  237. float v = Input.GetAxis(verticalAxis);
  238. // 转换为世界坐标
  239. //worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(h, v, Camera.main.nearClipPlane));
  240. // 设置物体的位置为摄像机坐标
  241. //transform.position = worldPosition;
  242. //posX = worldPosition.x - lastX;
  243. //ARcamera.transform.position = new Vector3(ARcamera.transform.position.x + worldPosition.x, 0, ARcamera.transform.position.z+ worldPosition.z);
  244. //if (ARcamera.transform.position.x > 557f)
  245. //{
  246. // //ARcamera.transform.position = new Vector3(3f, 0, 0);
  247. // if (ARcamera.transform.position.z > 557f)
  248. // {
  249. // ARcamera.transform.position = new Vector3(557f, 0, 557f);
  250. // }
  251. // else if (ARcamera.transform.position.z >= 0)
  252. // {
  253. // ARcamera.transform.position = new Vector3(557f, 0, worldPosition.z);
  254. // }
  255. // else
  256. // {
  257. // ARcamera.transform.position = new Vector3(557f, 0, 0f);
  258. // }
  259. //}
  260. //else if (ARcamera.transform.position.x >= 540f)
  261. //{
  262. // //ARcamera.transform.position = new Vector3(worldPosition.x, 0, 0);
  263. // if (ARcamera.transform.position.z > 557f)
  264. // {
  265. // ARcamera.transform.position = new Vector3(worldPosition.x, 0, 557f);
  266. // }
  267. // else if (ARcamera.transform.position.z >= 0)
  268. // {
  269. // ARcamera.transform.position = new Vector3(worldPosition.x, 0, worldPosition.z);
  270. // }
  271. // else
  272. // {
  273. // ARcamera.transform.position = new Vector3(worldPosition.x, 0, 0f);
  274. // }
  275. //}
  276. //else
  277. //{
  278. // //ARcamera.transform.position = new Vector3(-8f, 0, 0);
  279. // if (ARcamera.transform.position.z > 557f)
  280. // {
  281. // ARcamera.transform.position = new Vector3(540f, 0, 557f);
  282. // }
  283. // else if (ARcamera.transform.position.z >= 0)
  284. // {
  285. // ARcamera.transform.position = new Vector3(540f, 0, worldPosition.z);
  286. // }
  287. // else
  288. // {
  289. // ARcamera.transform.position = new Vector3(540f, 0, 0f);
  290. // }
  291. //}
  292. //txt2.text = ARcamera.transform.position.ToString("F2");
  293. lastX = worldPosition.x;
  294. // 获取sessionOrigin的位置
  295. //Vector3 position = sessionOrigin.transform.position;
  296. //// 获取sessionOrigin的转动角度
  297. //Quaternion rotation = sessionOrigin.transform.rotation;
  298. //// 如果需要以欧拉角形式获取角度
  299. //Vector3 eulerAngles = rotation.eulerAngles;
  300. //txt.text = eulerAngles.ToString("F2");
  301. //myText.text = position.ToString("F2");
  302. }
  303. }