U3D_TobaccoWarehouseISMDTSy.../Assets/Scripts/YL/Checkposition.cs

51 lines
1.5 KiB
C#

using Cysharp.Threading.Tasks;
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Checkposition : MonoBehaviour
{
/// <summary>
/// 位移的点位
/// </summary>
public Transform point;
/// <summary>
/// 位移的速度
/// </summary>
private float speed = 2f;
public async void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("Box"))
{
if (point.childCount == 0)
{
await MoveObjectToPosition(other.gameObject.transform, point.transform.position, speed);
other.gameObject.transform.SetParent(point);
BoxCollider boxCollider = transform.GetComponent<BoxCollider>();
if (boxCollider)
{
boxCollider.enabled = false;
}
BoxCollider points = point.GetComponent<BoxCollider>();
if (points)
{
points.enabled = true;
}
}
}
}
/// <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).AsyncWaitForCompletion();
}
}