Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        switch (state)
        {
        case State.Squash:
        {
            float heightChange = 1 - squashedHeight;
            float height       = squashScale.y;
            height -= heightChange * Time.deltaTime / squashDuration;

            if (height <= squashedHeight)
            {
                height = squashedHeight;
                state  = State.SquashedStill;
                timer  = 0f;
            }

            SetScale(height);

            break;
        }

        case State.SquashedStill:
            if (timer > remainSquashedDuration && recover)
            {
                timer = 0f;
                state = State.Recover;
            }

            timer += Time.deltaTime;

            break;

        case State.Recover:
        {
            SetScale(Easings.EaseOutElastic(squashedHeight, 1, timer / recoverDuration));
            timer += Time.deltaTime;
            if (timer >= recoverDuration)
            {
                CancelSquash();
            }

            break;
        }
        }

        if (state != State.Still)
        {
            transform.localScale = Vector3.Scale(baseScale, squashScale);
            Vector3 pivotPosition = basePosition - new Vector3(0, collision.bounds.extents.y, 0);
            Vector3 pivotOffset   = basePosition - pivotPosition;
            pivotPosition      = basePosition - new Vector3(0, collision.bounds.extents.y * baseScale.y, 0);
            transform.position = Vector3.Scale(pivotOffset, transform.localScale) + pivotPosition;
        }
    }