U3D_TobaccoWarehouseISMDTSy.../Assets/Scripts/YL/Transportbox.cs

158 lines
5.0 KiB
C#

using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
public class Transportbox : MonoBehaviour
{
/// <summary>
/// 物体位移到指定位置
/// </summary>
public Transform transportbox;
/// <summary>
/// 位移箱子速度
/// </summary>
public float speed = 5f;
/// <summary>
/// 提升机
/// </summary>
public GameObject Uptrans;
/// <summary>
/// 需要位移的点位
/// </summary>
public Transform point;
/// <summary>
/// 记住提升机初始位置
/// </summary>
public Transform pos;
/// <summary>
/// 箱子第一个位置点
/// </summary>
public Transform boxpos;
/// <summary>
/// 箱子第二个位置点
/// </summary>
public Transform box2;
/// <summary>
/// 提升机第四个点位
/// </summary>
public Transform box3;
/// <summary>
/// 码垛机提升抓手
/// </summary>
public GameObject tongs;
/// <summary>
/// 箱子位移第四个点位
/// </summary>
public Transform box4;
/// <summary>
/// 码垛机向下位移
/// </summary>
public Transform box5;
/// <summary>
/// 码垛机回原位
/// </summary>
public Transform box6;
void Start()
{
}
void Update()
{
}
/// <summary>
/// 有箱子触发
/// </summary>
/// <param name="other"></param>
private async void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Box"))
{
await Makeprogress(other.gameObject, Uptrans);
}
}
/// <summary>
/// 下箱
/// </summary>
/// <param name="box"></param>
/// <param name="hoister"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 入库烟箱
/// </summary>
/// <param name="point"></param>
/// <param name="upmachine"></param>
/// <returns></returns>
private async UniTask Moveup(GameObject point, GameObject upmachine)
{
await MoveObjectToPosition(point.transform, transportbox.transform.position, speed);
point.gameObject.transform.SetParent(null);
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<Boxinformation>();
GameObject daughter = point.transform.GetChild(0).gameObject;
daughter.transform.SetParent(null);
BoxCollider boxtwe = point.GetComponent<BoxCollider>();
boxtwe.size = new Vector3(1.3f, 1.3f, 1.3f);
boxtwe.center = new Vector3(0, 0.6f,0);
Boxinformation boxdata = daughter.AddComponent<Boxinformation>();
Assign(boxdata, box);
BoxCollider cillider = daughter.AddComponent<BoxCollider>();
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);
}
/// <summary>
/// 获取箱子身上参数
/// </summary>
/// <param name="box1"></param>
/// <param name="box2"></param>
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;
}
/// <summary>
/// 让箱子做对应的动画
/// </summary>
/// <param name="target">需要移动的物体</param>
/// <param name="destination">指定的位置</param>
/// <param name="speed">速度</param>
/// <returns></returns>
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();
}
}