Place packages on the roof of the car

Instead of destroying packages right away, decided to place them on the roof of the car. I’m destroying them after I deliver them to the customer.
Roof

[SerializeField] Vector3 packagePosition;
[SerializeField] Vector3 packageScale;

void OnTriggerEnter2D(Collider2D trigger) {
    switch (package) {
        case null when trigger.CompareTag("Package"):
            PackagePickedUp(trigger.transform.position);
            PlacePackage(trigger);
            break;
        case not null when trigger.CompareTag("Customer"):
            var customer = trigger.GetComponent<DeliveryObject>();
            if (customer != package.Customer) {
                return;
            }
            Destroy(package!.gameObject);
            package = null;
            Destroy(trigger.gameObject);
            PackageDelivered(trigger.transform.position);
            break;
    }

    void PlacePackage(Collider2D newPackage) {
        package = newPackage.GetComponent<DeliveryObject>();
        var packageTransform = package.transform;
        packageTransform.parent = transform;
        packageTransform.localPosition = packagePosition;
        packageTransform.localRotation = Quaternion.identity;
        packageTransform.localScale = packageScale;
        package.SortingOrder = 3;
    }
}
4 Likes

Great code! Awesome job!

Privacy & Terms