勇哥注:
轴的移动很简单,就是Translate。
整个XZ取放如果想自动连在一起运动,在协程里应用StartCoroutine进行顺序动作的定义即可。
下面这个例子中,还用到了上节所讲的夹子代码。
(图1)
Safe Height
Take Product Depth 取料时Z轴的深度
Release Product Length 放料时的深度,因为是放到另一个拉带上,所以要单独控制深度。
Release Product Length 这个当前设置的是3个横向位置,它是一个数组。
Feed In 是自动运行的开关。
Working是防重复操作。
Finished
Lift Time 横向移动的速度
Carrying Time 竖向取料的运动速度
Delta Time
Product Index 这个是横向移动位置的那个数组的指针,在自动运行时它会自动累计。
(国2)
源码:
Lifting是横向移动的协程,Carrying是上下移动的协程。
Working协程定义的自动运行时的动作序列。
最后还定义了一些调试功能,1夹子动作 2横向移动 3上下动作
public class Lift_Function : MonoBehaviour { public GameObject lifter; public ConveyorBelt conveyorBelt_Function; public float safeHeight= 0.18f; public float takeProductDepth= 0.202f; public float releaseProductDepth= 0.3f; public float[] releaseProductLength; public bool feedIn; public bool working; public bool finished; public float liftTime = 2; public float carryingTime = 2; public float deltaTime = 0.1f; public int productIndex = 0; Clip_Function clipper; [Range(-1, 1)] int moving_V ; [Range(-1, 1)] int moving_H; private void Start() { clipper = GetComponent<Clip_Function>(); working = false; finished = true; feedIn = false; } IEnumerator Lifting() { if (working == false) { if (moving_V == 0) { moving_V = 1; } else { moving_V = -moving_V; } working = true; float tempDepth; if (lifter.transform.localPosition.z == 0) { tempDepth = takeProductDepth; } else { tempDepth = releaseProductDepth; } for (int i = 0; i < 60; i++) { lifter.transform.Translate(new Vector3(0, -moving_V * tempDepth / 60, 0)); yield return new WaitForSecondsRealtime(liftTime / 60); } working = false; } } IEnumerator Carrying() { if (working == false) { if (moving_H == 0) { moving_H = 1; } else { moving_H = -moving_H; } if (lifter.transform.localPosition.y > safeHeight) { working = true; for (int i = 0; i < 60; i++) { lifter.transform.Translate(new Vector3(0, 0, -moving_H * releaseProductLength[productIndex] / 60)); yield return new WaitForSecondsRealtime(carryingTime / 60); } working = false; } } } IEnumerator Working() { feedIn = false; yield return StartCoroutine("Lifting"); yield return StartCoroutine(clipper.ClipFunction()); yield return StartCoroutine("Lifting"); yield return StartCoroutine("Carrying"); yield return StartCoroutine("Lifting"); yield return StartCoroutine(clipper.ClipFunction()); yield return StartCoroutine("Lifting"); yield return StartCoroutine("Carrying"); feedIn = true; if (productIndex < releaseProductLength.Length-1) { productIndex += 1; } else { productIndex = 0; feedIn = false; finished = true; } } private void OnTriggerStay(Collider other) { conveyorBelt_Function.feedIn = false; if (feedIn && (!working)) { StartCoroutine("Working"); } } private void OnTriggerExit(Collider other) { conveyorBelt_Function.feedIn = true; } #region debug void LiftWorking() { if(working==false) StartCoroutine("Lifting"); } void CarryWorking() { if (working == false) StartCoroutine("Carrying"); } private void Update() { if (Input.GetKeyDown(KeyCode.Alpha2) ) { LiftWorking(); } if (Input.GetKeyDown(KeyCode.Alpha3)) { CarryWorking(); } } #endregion }

