Ejemplo n.º 1
0
    IEnumerator CoinCollect()
    {
        PlayCoinPickupSound();

        if (coinTrail == null)
        {
            coinTrail = Instantiate(coinTrailPrefab, this.transform);
        }

        if (rewardCoins) // coins earned through a challenge shouldn't have a trail
        {
            coinTrail.GetComponent <TrailRenderer>().sortingOrder = 101;
        }

        var     elapsedTime   = 0f;
        var     startingPos   = this.transform.position;
        var     timeToCollect = 0.2f;
        Vector2 arcPoint      = startingPos + new Vector3(target.transform.position.x - startingPos.x, target.transform.position.y - startingPos.y + Random.Range(-1f, 2.5f), 0);

        while (elapsedTime < timeToCollect)
        {
            var timer = (elapsedTime / timeToCollect);

            // moves the coins in an arc with a lerp between two lerps, lerpception?
            Vector2 m1   = Vector2.Lerp(startingPos, arcPoint, timer);
            Vector2 m2   = Vector2.Lerp(arcPoint, target.transform.position, timer);
            var     move = Vector2.Lerp(m1, m2, timer);

            this.transform.position = move;


            elapsedTime += Time.deltaTime;
            yield return(null);
        }

        if (elapsedTime >= timeToCollect)
        {
            PlayerPrefs.SetFloat("Coins", PlayerPrefs.GetFloat("Coins") + 1);
            gm.RefreshCoinCount();

            CoinParticle(target.transform);

            coinTrail.GetComponent <TrailRenderer>().emitting = false;

            this.GetComponent <SpriteRenderer>().enabled = false;
            gm.StartCoroutine("CoinCountBump");
            Destroy(this.gameObject, 0.3f);
        }
    }