using Cysharp.Threading.Tasks; using DG.Tweening; using Unity.VisualScripting; using UnityEditor.SceneManagement; using UnityEngine; public class Transportbox : MonoBehaviour { /// /// 物体位移到指定位置 /// public Transform transportbox; /// /// 位移箱子速度 /// public float speed = 5f; /// /// 提升机 /// public GameObject Uptrans; /// /// 需要位移的点位 /// public Transform point; /// /// 记住提升机初始位置 /// public Transform pos; /// /// 箱子第一个位置点 /// public Transform boxpos; /// /// 箱子第二个位置点 /// public Transform box2; /// /// 提升机第四个点位 /// public Transform box3; /// /// 码垛机提升抓手 /// public GameObject tongs; /// /// 箱子位移第四个点位 /// public Transform box4; /// /// 码垛机向下位移 /// public Transform box5; /// /// 码垛机回原位 /// public Transform box6; void Start() { } void Update() { } /// /// 有箱子触发 /// /// private async void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Box")) { await Makeprogress(other.gameObject, Uptrans); } } /// /// 下箱 /// /// /// /// private async UniTask Makeprogress(GameObject box, GameObject hoister) { await MoveObjectToPosition(hoister.transform, point.transform.position, speed); box.gameObject.transform.position = new Vector3(box.transform.position.x, box.transform.position.y + 0.8f, box.transform.position.z); box.gameObject.transform.SetParent(null); await Moveup(box.gameObject, hoister); } /// /// 入库烟箱 /// /// /// /// private async UniTask Moveup(GameObject point, GameObject upmachine) { await MoveObjectToPosition(point.transform, transportbox.transform.position, speed); point.transform.SetParent(upmachine.transform); await MoveObjectToPosition(upmachine.transform, pos.transform.position, speed); point.transform.SetParent(null); await MoveObjectToPosition(point.transform, boxpos.transform.position, speed); await MoveObjectToPosition(point.transform, box2.transform.position, speed); Boxinformation box = point.GetComponent(); GameObject daughter = point.transform.GetChild(0).gameObject; daughter.transform.SetParent(null); BoxCollider boxtwe = point.GetComponent(); boxtwe.size = new Vector3(1.3f, 1.3f, 1.3f); boxtwe.center = new Vector3(0, 0.6f,0); Boxinformation boxdata = daughter.AddComponent(); Assign(boxdata, box); BoxCollider cillider = daughter.AddComponent(); cillider.size = new Vector3(1.3f, 1.3f, 1.3f); daughter.tag = "Box"; daughter.transform.SetParent(tongs.transform); await MoveObjectToPosition(tongs.transform, box3.transform.position, speed); await MoveObjectToPosition(point.transform, box4.transform.position, speed); await MoveObjectToPosition(tongs.transform, box5.transform.position, speed); daughter.transform.SetParent(null); await MoveObjectToPosition(tongs.transform, box6.transform.position, speed); } /// /// 获取箱子身上参数 /// /// /// public void Assign(Boxinformation box1, Boxinformation box2) { box1.ID = box2.ID; box1.type = box2.type; box1.locationId = box2.locationId; box1.description = box2.description; box1.locationState = box2.locationState; box1.storageState = box2.storageState; box1.layer = box2.layer; box1.row = box2.row; box1.column = box2.column; box1.specialFlag = box2.specialFlag; box1.palletNum = box2.palletNum; box1.itemType = box2.itemType; box1.isSpecial = box2.isSpecial; } /// /// 让箱子做对应的动画 /// /// 需要移动的物体 /// 指定的位置 /// 速度 /// private async UniTask MoveObjectToPosition(Transform target, Vector3 destination, float speed) { float duration = Vector3.Distance(target.position, destination) / speed; await target.DOMove(destination, duration).SetEase(Ease.InOutQuad).AsyncWaitForCompletion(); } }