123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- using UnityEngine;
- using UnityEngine.AI;
- using System.Collections;
- using UnityEngine.UI;
- using UnityEngine.XR.ARFoundation;
- using TMPro;
- //using UnityEngine.UIElements;
- //using UnityEditor.PackageManager.Requests;
- public class AutoMove : MonoBehaviour
- {
- public Vector3[] points;
- private ARSessionOrigin sessionOrigin;
- private int destPoint = 0;
- private NavMeshAgent agent;
- private Vector3 randomCoordinate;
- private bool isWait = true; //小狗是否在等待
- private bool isActive = false; //小狗是否可以行动
- private bool isBack = false; //小狗是否可以回来
- private Animator animator;
- private Text txt2;
- private Text txt;
- private Button myButton;
- private Button sittingBtn;
- private Button angryBtn;
- private float x = 0;
- private float z = 0;
- public string horizontalAxis = "Horizontal"; // 在输入管理器中设置的水平轴名称
- public string verticalAxis = "Vertical"; // 在输入管理器中设置的垂直轴名称
- public Vector3 worldPosition = new Vector3(0f, 0f, 0f);
- private int randomNumber = 0;
- private int idleTime = 4; //待机时间
- public int 体力值 = 35;
- private float lastX;
- private float posX;
- public GameObject ARcamera;
- void Start()
- {
- //Debug.LogError("这个一条信息");
- sessionOrigin = FindObjectOfType<ARSessionOrigin>();
- agent = GetComponent<NavMeshAgent>();
- animator = GetComponent<Animator>();
- // 禁用自动制动将允许点之间的
- // 连续移动(即,代理在接近目标点时
- // 不会减速)。
- agent.autoBraking = false;
- //txt = GameObject.Find("txt").GetComponent<Text>();
- //txt2 = GameObject.Find("txt2").GetComponent<Text>();
- x = this.transform.position.x;
- z = this.transform.position.z;
- //myButton = GameObject.Find("backBtn").GetComponent<Button>();
- //myButton.onClick.AddListener(OnButtonClick);
- //sittingBtn = GameObject.Find("sittingBtn").GetComponent<Button>();
- //sittingBtn.onClick.AddListener(OnSittingClick);
- //angryBtn = GameObject.Find("angryBtn").GetComponent<Button>();
- //angryBtn.onClick.AddListener(OnAngryBtnClick);
- GotoNextPoint();
- }
- void OnSittingClick()
- {
- animator.SetBool("isRun", false);
- animator.SetInteger("state", 3);
- }
- void OnAngryBtnClick()
- {
- animator.SetBool("isRun", false);
- animator.SetInteger("state", 1);
- }
- void OnButtonClick()
- {
- if(myButton.GetComponentInChildren<Text>().text == "小狗回来") {
- x = worldPosition.x;
- z = worldPosition.z;
-
- animator.SetBool("isRun", true);
- randomCoordinate = new Vector3(0, -1.1f, 1.2f);
- //txt.text = randomCoordinate.ToString("F2");
- agent.destination = randomCoordinate;
- isBack = true;
- isWait = false;
- // 修改按钮文本
- myButton.GetComponentInChildren<Text>().text = "自己玩耍";
- } else
- {
- isActive = false;
- isWait = true;
- myButton.GetComponentInChildren<Text>().text = "小狗回来";
- }
- }
- void GotoNextPoint()
- {
- // 如果未设置任何点,则返回
- if (points.Length == 0)
- return;
- //将代理设置为前往当前选定的目标。
- agent.destination = points[destPoint];
- //选择数组中的下一个点作为目标,
- // 如有必要,循环到开始。
- destPoint = (destPoint + 1) % points.Length;
- }
- void followPosition()
- {
- StartCoroutine(DelayedFunction());
-
-
- }
- IEnumerator DelayedFunction1()
- {
- yield return new WaitForSeconds(5);
- followPosition();
- }
- IEnumerator DelayedFunction()
- {
- isWait = false;
- if (体力值 >= 60)
- {
- animator.SetBool("isrun", true);
- agent.speed = 1f;
- animator.SetInteger("state", 1);
- //randomCoordinate = new Vector3(Random.Range(x - 4, x + 4), -1.5f, Random.Range(z - 4, z + 4));
- randomCoordinate = new Vector3(Random.Range(540f, 554f), 0f, Random.Range(540f, 554f));
- //Debug.Log("Generated random coordinate: " + randomCoordinate);
- agent.destination = randomCoordinate;
- yield return new WaitForSeconds(1);
- } else if (体力值 >= 30)
- {
- animator.SetBool("isrun", true);
- agent.speed = 0.5f;
- animator.SetInteger("state", 2);
- animator.SetBool("isrun", true);
- //randomCoordinate = new Vector3(Random.Range(x - 4, x + 4), -1.5f, Random.Range(z - 4, z + 4));
- randomCoordinate = new Vector3(Random.Range(540f, 554f), 0f, Random.Range(540f, 554f));
- Debug.Log("Generated random coordinate: " + randomCoordinate);
- agent.destination = randomCoordinate;
- yield return new WaitForSeconds(1);
- } else
- {
- animator.SetBool("isrun", false);
- animator.SetBool("isIdle", true);
- animator.SetInteger("idle", Random.Range(0, 6));
- yield return new WaitForSeconds(5);
- }
- isWait = true;
- }
- // 定义协程函数
- IEnumerator DelayedFunction3()
- {
- // 延迟5秒
- agent.speed = 0.5f;
- isWait = false;
- //animator.SetBool("isRun", false);
- randomNumber = Random.Range(1, 3);
- animator.SetInteger("state", randomNumber);
- if(randomNumber == 0)
- {
- animator.SetBool("isIdle", true);
- animator.SetInteger("idle", Random.Range(0, 6));
- yield return new WaitForSeconds(1);
- } else
- {
-
- //randomCoordinate = new Vector3(Random.Range(x - 4, x + 4), -1.5f, Random.Range(z - 4, z + 4));
- randomCoordinate = new Vector3(Random.Range(540f, 554f), 0f, Random.Range(540f, 554f));
- //Debug.Log("Generated random coordinate: " + randomCoordinate);
- //Debug.Log(randomCoordinate);
- agent.destination = randomCoordinate;
- isWait = false;
- yield return new WaitForSeconds(1);
- }
- if(randomNumber == 1)
- {
- agent.speed = 1;
- }
- //float waitTime = Random.Range(idleTime / 2, idleTime * 2);
-
- if (!isBack)
- {
- if(randomNumber != 0)
- {
- ////randomCoordinate = new Vector3(Random.Range(x - 4, x + 4), -1.5f, Random.Range(z - 4, z + 4));
- //randomCoordinate = new Vector3(Random.Range(x - 8, x + 8), 0f, Random.Range(z - 8, z + 8));
- ////Debug.Log("Generated random coordinate: " + randomCoordinate);
- //agent.destination = randomCoordinate;
- }
- isWait = true;
- //animator.SetBool("isRun", true);
- animator.SetBool("isIdle", false);
- }
- }
- void Update()
- {
- //myText.text = this.transform.position.ToString("F2");
- //txt.text = z.ToString("F2");
- //当代理接近当前目标点时,
- // 选择下一个目标点。
- //txt.text = isWait.ToString();
- //txt2.text = isBack.ToString();
- if (体力值 >= 30)
- {
- animator.SetBool("isrun", true);
- animator.SetBool("isIdle", false);
- agent.speed = 0.5f;
- if (体力值 >= 60)
- {
- agent.speed = 1f;
- }
- if (!agent.pathPending && agent.remainingDistance < 0.05f && !isBack)
- {
- if (isWait)
- {
- animator.SetInteger("state", 0);
- //animator.SetBool("isIdle", true);
- //animator.SetInteger("idle", Random.Range(0, 6));
- StartCoroutine(DelayedFunction());
- //followPosition();
- }
- }
- if (!agent.pathPending && agent.remainingDistance < 0.05f && isBack)
- {
- randomCoordinate = new Vector3(0, -1.1f, 1f);
- agent.destination = randomCoordinate;
- isBack = false;
- isActive = true;
- }
- if (!agent.pathPending && agent.remainingDistance < 0.05f && !isBack)
- {
- if (isActive)
- {
- animator.SetBool("isrun", false);
- animator.SetInteger("state", 0);
- isActive = false;
- }
- }
- } else
- {
- agent.speed = 0f;
- animator.SetInteger("state", 0);
- animator.SetBool("isrun", false);
- StartCoroutine(DelayedFunction());
- }
-
- // 获取水平和垂直轴的值
- float h = Input.GetAxis(horizontalAxis);
- float v = Input.GetAxis(verticalAxis);
- // 转换为世界坐标
- //worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(h, v, Camera.main.nearClipPlane));
- // 设置物体的位置为摄像机坐标
- //transform.position = worldPosition;
- //posX = worldPosition.x - lastX;
- //ARcamera.transform.position = new Vector3(ARcamera.transform.position.x + worldPosition.x, 0, ARcamera.transform.position.z+ worldPosition.z);
- //if (ARcamera.transform.position.x > 557f)
- //{
- // //ARcamera.transform.position = new Vector3(3f, 0, 0);
- // if (ARcamera.transform.position.z > 557f)
- // {
- // ARcamera.transform.position = new Vector3(557f, 0, 557f);
- // }
- // else if (ARcamera.transform.position.z >= 0)
- // {
- // ARcamera.transform.position = new Vector3(557f, 0, worldPosition.z);
- // }
- // else
- // {
- // ARcamera.transform.position = new Vector3(557f, 0, 0f);
- // }
- //}
- //else if (ARcamera.transform.position.x >= 540f)
- //{
- // //ARcamera.transform.position = new Vector3(worldPosition.x, 0, 0);
- // if (ARcamera.transform.position.z > 557f)
- // {
- // ARcamera.transform.position = new Vector3(worldPosition.x, 0, 557f);
- // }
- // else if (ARcamera.transform.position.z >= 0)
- // {
- // ARcamera.transform.position = new Vector3(worldPosition.x, 0, worldPosition.z);
- // }
- // else
- // {
- // ARcamera.transform.position = new Vector3(worldPosition.x, 0, 0f);
- // }
- //}
- //else
- //{
- // //ARcamera.transform.position = new Vector3(-8f, 0, 0);
- // if (ARcamera.transform.position.z > 557f)
- // {
- // ARcamera.transform.position = new Vector3(540f, 0, 557f);
- // }
- // else if (ARcamera.transform.position.z >= 0)
- // {
- // ARcamera.transform.position = new Vector3(540f, 0, worldPosition.z);
- // }
- // else
- // {
- // ARcamera.transform.position = new Vector3(540f, 0, 0f);
- // }
- //}
- //txt2.text = ARcamera.transform.position.ToString("F2");
- lastX = worldPosition.x;
- // 获取sessionOrigin的位置
- //Vector3 position = sessionOrigin.transform.position;
- //// 获取sessionOrigin的转动角度
- //Quaternion rotation = sessionOrigin.transform.rotation;
- //// 如果需要以欧拉角形式获取角度
- //Vector3 eulerAngles = rotation.eulerAngles;
- //txt.text = eulerAngles.ToString("F2");
- //myText.text = position.ToString("F2");
- }
- }
|