Example #1
0
    private IEnumerator AddKeyCoroutine(KeyPickup keyPickup)
    {
        InputKey newKey = Instantiate(keyPickup.GetKeyPrefab(), keyParent);

        newKey.transform.SetAsLastSibling();

        // keyPickup is in world space while newKey (a RectTransform) is in canvas space, so the following properties need to be transformed
        Vector3    startPosition = Camera.main.WorldToScreenPoint(keyPickup.transform.position);
        Quaternion startRotation = keyPickup.transform.rotation * Quaternion.Inverse(Camera.main.transform.rotation);
        float      scaleFactor   = 1.0f / (Camera.main.orthographicSize * 2);

        scaleFactor *= CanvasScale.Instance.GetReferenceHeight();
        scaleFactor /= newKey.GetComponent <RectTransform>().rect.height;
        Vector3 startScale = keyPickup.transform.localScale * scaleFactor;

        Destroy(keyPickup.gameObject);


        float f = 0;

        while (f < 1)
        {
            f += Time.deltaTime / 0.6f;
            if (f > 1)
            {
                f = 1;
            }

            float smoothedF = Mathf.SmoothStep(0, 1, f);

            newKey.transform.position   = Vector3.Lerp(startPosition, keySpawnPoint.position, smoothedF);
            newKey.transform.rotation   = Quaternion.Slerp(startRotation, keySpawnPoint.localRotation, smoothedF);
            newKey.transform.localScale = Vector3.Lerp(startScale, keySpawnPoint.localScale, smoothedF);

            yield return(null);
        }

        inputKeys.Add(newKey);

        if (numberOfCollectedFragments > 3)
        {
            AudioManager.Instance.PlayNextMusic();
        }
    }