void OnCollisionEnter2D(Collision2D collision)
    {
        //ignore same-tagged enemies for maximum mayhem
        if (tag != collision.collider.tag)
        {
            //need to get to the parent gameobject of what we hit since all important colliders are children of their objects
            GameObject hitObject;
            Transform  ptrans = collision.collider.transform.parent;
            if (ptrans != null)
            {
                hitObject = ptrans.gameObject; //most cases this will be true
            }
            else
            {
                hitObject = collision.collider.gameObject; //on failure forget about parent
            }

            //else innanimate object, just destroy
            GameObject        go        = Instantiate(hitEffect, transform.position, Quaternion.identity);
            ExplosionBehavior explosion = go.GetComponent <ExplosionBehavior>();
            explosion.damage = damage;

            //tag it
            explosion.tag = tag;

            explosion.Eval();
            GameObject.Destroy(gameObject);
        }
        else
        {
            //ignore and reset to originals
            Physics2D.IgnoreCollision(collision.collider, collision.otherCollider);
            myRB2.velocity        = initialVelocity;
            myRB2.angularVelocity = 0;
            transform.rotation    = rotation;
        }
    }
Exemple #2
0
    void HandleAttack()
    {
        if (isAttacking && !startedAttack)  //trying to attack, start of new attack
        {
            myAnim.SetBool("isAttacking", true);
            chargeTimer   = chargeDelay;
            startedAttack = true;
        }
        else if (isAttacking && startedAttack && chargeTimer <= 0 && chargeEffectInstance == null) //still charging, timer done, no charge made, make charge effect
        {
            chargeEffectInstance = Instantiate(chargeEffect, transform);
            float x, y;
            if (myAnim.GetInteger("Vertical") > 0)
            {
                x = chargePositionU.transform.localPosition.x;
                y = chargePositionU.transform.localPosition.y;
            }
            else
            {
                x = chargePositionD.transform.localPosition.x;
                y = chargePositionD.transform.localPosition.y;
            }
            if (mySprite.flipX)
            {
                x = -x;
            }
            chargeEffectInstance.transform.localPosition = new Vector3(x, y, 0);
            audioMan.playSFX("ready", transform.position);
        }
        else if (!isAttacking && startedAttack) //execute attack
        {
            //calculate direction to attack
            float x;
            if (mySprite.flipX)
            {
                x = 1;
            }
            else
            {
                x = -1;
            }
            Vector3 attackDirection = new Vector3(x, myAnim.GetInteger("Vertical"), 0);
            attackDirection.Normalize();

            if (projectile == null) //melee character
            {
                GameObject        go  = Instantiate(hitEffect, transform.position, Quaternion.identity);
                ExplosionBehavior exp = go.GetComponent <ExplosionBehavior>();

                //set position
                exp.transform.position += attackDirection * 0.5f;

                //tag it
                exp.tag = tag;

                //set damage
                if (chargeTimer > 0) //early attack
                {
                    exp.damage = baseDamage;
                }
                else //charged attack
                {
                    exp.damage = Mathf.RoundToInt(baseDamage * chargeMultiplier);
                }
                exp.Eval();
            }
            else //projectile character
            {
                //try to get input direction
                if (dirInput.magnitude > 0)
                {
                    attackDirection = new Vector3(dirInput.x, dirInput.y, 0).normalized;
                }

                GameObject         go = Instantiate(projectile, transform.position, Quaternion.FromToRotation(projectile.transform.right, attackDirection));
                ProjectileBehavior pb = go.GetComponent <ProjectileBehavior>();

                //setposition
                go.transform.position += attackDirection * 0.5f;;

                //make sure it is tagged right
                go.tag = tag;

                //set damage & velocity
                if (chargeTimer > 0) //early attack
                {
                    pb.damage = baseDamage;
                    pb.init(attackDirection * arrowSpeed);
                }
                else //charged attack
                {
                    pb.damage = Mathf.RoundToInt(baseDamage * chargeMultiplier);
                    pb.init(attackDirection * arrowSpeed * chargeMultiplier);
                }
            }

            //set anim
            myAnim.SetBool("isAttacking", false);

            //finish chargeffect
            if (chargeEffectInstance != null)
            {
                chargeEffectInstance.GetComponent <Animator>().SetTrigger("done");
            }

            //reset bool
            startedAttack = false;
        }
        //else shouuld be no change, let timers tick
    }