123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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<Animator>();
- 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"));
- // 转身
- // 行走, 进入下一个待定位置
- }
- /// <summary>
- /// 定时切换状态
- /// </summary>
- /// <param name="times"></param>
- /// <param name="state"></param>
- /// <returns></returns>
- IEnumerator Timing( float times, string state)
- {
- yield return new WaitForSeconds(0.1f);
- isWalk = false;
- yield return new WaitForSeconds(times-0.1f);
- CutState(state);
- }
- /// <summary>
- /// 切换状态
- /// </summary>
- /// <param name="state"></param>
- 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"));
- }
- }
|