Exemple #1
0
    private void HandleAttack()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // Attack
            SetStateAttacking();

            Vector3 attackDir = (GetMouseWorldPosition() - GetPosition()).normalized;

            EnemyHandler enemyHandler = EnemyHandler.GetClosestEnemy(GetPosition() + attackDir * 4f, 20f);
            bool         hitEnemy;
            if (enemyHandler != null)
            {
                //enemyHandler.Damage(this);
                hitEnemy           = true;
                attackDir          = (enemyHandler.GetPosition() - GetPosition()).normalized;
                transform.position = enemyHandler.GetPosition() + attackDir * -12f;
            }
            else
            {
                hitEnemy           = false;
                transform.position = transform.position + attackDir * 4f;
            }

            float attackAngle = GetAngleFromVectorFloat(attackDir);

            // Play attack animation
            if (characterBase.IsPlayingPunchAnimation())
            {
                // Play Kick animation since punch animation is currently active
                characterBase.PlayKickAnimation(attackDir, (Vector3 impactPosition) => {
                    if (hitEnemy)
                    {
                        impactPosition          += GetVectorFromAngle((int)attackAngle) * 4f;
                        Transform impactEffect   = Instantiate(GameAssets.i.pfImpactEffect, impactPosition, Quaternion.identity);
                        impactEffect.eulerAngles = new Vector3(0, 0, attackAngle - 90);
                    }
                }, SetStateNormal);
            }
            else
            {
                // Play Punch animation
                characterBase.PlayPunchAnimation(attackDir, (Vector3 impactPosition) => {
                    if (hitEnemy)
                    {
                        impactPosition          += GetVectorFromAngle((int)attackAngle) * 4f;
                        Transform impactEffect   = Instantiate(GameAssets.i.pfImpactEffect, impactPosition, Quaternion.identity);
                        impactEffect.eulerAngles = new Vector3(0, 0, attackAngle - 90);
                    }
                }, SetStateNormal);
            }
        }
    }