using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Robot : MonoBehaviour { public float Speed = 1f; private Animation mAnimation; private Animator mAnimator; private string stateName; private bool isWalk; private string robotState; /* * Waving * Point_left */ private void Start() { mAnimator = GetComponent(); StateOne(); isWalk = false; } private void Update() { if(isWalk) { transform.Translate(Vector3.forward * Time.deltaTime * Speed,Space.Self); } } private void OnTriggerEnter(Collider other) { if(other.tag == "RobotBox") { switch (other.name) { case "RobotOne": transform.DOLocalRotate(new Vector3(0, 90, 0), 1f); StartCoroutine(Timing(1.5f, "Point_left")); break; case "RobotTwo": transform.DOLocalRotate(new Vector3(0, 90, 0), 1f); StartCoroutine(Timing(1.5f, "Point_right")); break; case "RobotThree": transform.DOLocalRotate(new Vector3(0, 90, 0), 1f); StartCoroutine(Timing(1.5f, "Point_left")); break; case "RobotFour": transform.DOLocalRotate(new Vector3(0, 90, 0), 1f); StartCoroutine(Timing(1.5f, "Point_left")); break; default: break; } robotState = other.name; } } private void StateOne() { CutState("Waving"); StartCoroutine(Timing(1.5f, "Point_left")); StartCoroutine(StateTwo(4f)); } IEnumerator StateTwo(float times) { yield return new WaitForSeconds(times); transform.DOLocalRotate(new Vector3(0, -90, 0), 1f); StartCoroutine(Timing(1f, "Walk")); // 转身 // 行走, 进入下一个待定位置 } /// /// 定时切换状态 /// /// /// /// IEnumerator Timing( float times, string state) { yield return new WaitForSeconds(0.1f); isWalk = false; yield return new WaitForSeconds(times-0.1f); CutState(state); } /// /// 切换状态 /// /// private void CutState(string state) { if(stateName!=null) mAnimator.SetBool(stateName, false); if (state == "Walk") isWalk = true; mAnimator.SetBool(state, true); stateName = state; } public void Walk(string posName) { switch (posName) { case "Gongye": if (robotState != "RobotOne") return; break; case "Subway": if (robotState != "RobotTwo") return; break; case "Chemical": if (robotState != "RobotThree") return; break; default: return; } transform.DOLocalRotate(new Vector3(0, -90, 0), 1f); StartCoroutine(Timing(1f, "Walk")); } }