Example #1
0
    private IEnumerator GlassFillCoroutine()
    {
        isFilling = true;
        // Spawn drink
        GameObject drink = Instantiate(drinkPrefab, drinkSpawnPosition.position, drinkSpawnPosition.rotation);

        drink.GetComponent <Whiskey>().filled = 0;
        yield return(new WaitForSeconds(0.1f));

        // Fill glass
        float filled = 0f;

        while (filled < 1)
        {
            filled += Time.deltaTime / fillTime;
            drink.GetComponent <Whiskey>().filled = Mathf.Min(filled, 1);
            yield return(null);
        }

        float startTime = Time.time;

        while (Vector3.Distance(drink.transform.position, tray.transform.position) > 0.1f)
        {
            if (!tray.isCarried)
            {
                Destroy(drink);
                isFilling = false;
                yield break;
            }

            float moveDistance = Vector3.Distance(drink.transform.position, tray.transform.position) / (startTime + drinkMoveTime - Time.time) * Time.deltaTime;
            drink.transform.position = Vector3.MoveTowards(drink.transform.position, tray.transform.position, moveDistance);
            yield return(null);
        }

        Destroy(drink);
        tray.AddDrink(drinkType);
        isFilling = false;
    }