Пример #1
0
    private IEnumerator Dash()
    {
        state.StartDash();
        gameObject.layer = LayerMask.NameToLayer("Dash");

        if (state.Carrying)
        {
            DropCarriedObject();
        }

        Vector2 dashStartPos = rb.position;
        Vector2 dashDir      = moveDir.normalized;
        Vector2 dashEndPos   = dashStartPos + dashDir * m_DashDistance;

        bool    collision      = false;
        Vector2 collisionPoint = Vector2.zero;

        int          layerMask = LayerMask.GetMask("Impassable");
        RaycastHit2D hit       = Physics2D.BoxCast(dashStartPos, new Vector2(0.60f, 0.20f), 0.0f, dashDir, m_DashDistance, layerMask);

        if (hit.collider != null)
        {
            collision      = true;
            collisionPoint = hit.point - dashDir * 0.5f;
        }

        Vector2 startVelocity = rb.velocity;

        rb.velocity = Vector2.zero;

        float dashTimer = 0.0f;

        while (dashTimer < m_DashDuration)
        {
            float fraction = m_DashCurve.Evaluate(dashTimer / m_DashDuration);
            if (collision && fraction >= hit.fraction)
            {
                break;
            }
            else
            {
                rb.MovePosition(Vector2.Lerp(dashStartPos, dashEndPos, fraction));
            }
            dashTimer += TimeManager.instance.deltaTime;
            yield return(new WaitForLaundromatSeconds(0.0f));
        }

        if (collision)
        {
            AudioManager.instance.PlaySound(SoundName.Collision);
            Vector2 reflected = Vector2.Reflect(dashDir, hit.normal);
            dashEndPos = collisionPoint + reflected * m_DashReboundDistance;
            float reboundTimer = 0.0f;
            while (reboundTimer < m_DashReboundDuration)
            {
                rb.MovePosition(Vector2.Lerp(collisionPoint, dashEndPos, m_DashCurve.Evaluate(reboundTimer / m_DashReboundDuration)));
                reboundTimer += TimeManager.instance.deltaTime;
                yield return(new WaitForLaundromatSeconds(0.0f));
            }
        }

        state.EndDash();
        gameObject.layer = LayerMask.NameToLayer("Player");

        StartCoroutine(state.DashCooldown(m_DashCooldown));

        rb.MovePosition(dashEndPos);
        if (!collision)
        {
            rb.velocity = startVelocity;
        }
    }