Forum begins after the advertisement:


[Part 3 Bug Fixes] Double Damage, Faulty Recoil, Infinite Attack Speed

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 3 Bug Fixes] Double Damage, Faulty Recoil, Infinite Attack Speed

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #14551
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    This is a supplementary post written for Part 3 of our Metroidvania series, and it aims to address 2 things:

    1. Missing information in the video, and;
    2. Address common issues / questions that readers run into, and possible solutions for these questions.

    For convenience, below are the links to the article and the video:


    Table of Contents

    Missing Information in the Video

    1. Enemy takes more/twice the damage due to BoxCollider2D
    2. Enemy continues to move while being recoiled
    3. Infinite Attack Speed

    Common Issues

    1. ___ does not take damage
    2. Player does not recoil when hitting enemies

    Missing Information in the Video

    1. Enemy takes more/twice the damage due to BoxCollider2D.

    [You can choose between adding a code to the Enemy.cs or the PlayerController.cs. In the series, we have decided to go with the PlayerController.cs method, but both methods work.]

    void Hit(Transform _attackTransform, Vector2 _attackArea, ref bool _recoilDir, float _recoilStrength)
    {
        Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer);
        List<Enemy> hitEnemies = new List<Enemy>();
    
        if(objectsToHit.Length > 0)
        {
            _recoilDir = true;
        }
    
        for(int i = 0; i < objectsToHit.Length; i++)
        {
            if (objectsToHit[i].GetComponent<Enemy>() != null)
            {
                objectsToHit[i].GetComponent<Enemy>().EnemyHit(damage, (transform.position - objectsToHit[i].transform.position).normalized, _recoilStrength);
            }
    
            Enemy e = objectsToHit[i].GetComponent<Enemy>();
            if(e && !hitEnemies.Contains(e)) {
                e.EnemyHit(damage, (transform.position - objectsToHit[i].transform.position).normalized, _recoilStrength);
                hitEnemies.Add(e);
            }
        }
    }

    2. Enemy continues to move while being recoiled.

    [This issue is caused by a missing code]

    • Set `isRecoiling` to true in the Enemy.cs `EnemyHit()` method.
        public virtual void EnemyHit(float _damageDone, Vector2 _hitDirection, float _hitForce)
        {
            health -= _damageDone;
            if(!isRecoiling)
            {
                rb.AddForce(-_hitForce * recoilFactor * _hitDirection);
                isRecoiling = True;
            }
        }

    3. Infinite Attack Speed.

    [This issue is caused by a missing code]

    • Change `private float timeBetweenAttack;` to `[SerializeField] private float timeBetweenAttack;`. in PlayerController.cs
        [Header("Attack Settings:")]
        [SerializeField] private Transform SideAttackTransform; //the middle of the side attack area
        [SerializeField] private Vector2 SideAttackArea; //how large the area of side attack is
    
        [SerializeField] private Transform UpAttackTransform; //the middle of the up attack area
        [SerializeField] private Vector2 UpAttackArea; //how large the area of side attack is
    
        [SerializeField] private Transform DownAttackTransform; //the middle of the down attack area
        [SerializeField] private Vector2 DownAttackArea; //how large the area of down attack is
    
        [SerializeField] private LayerMask attackableLayer; //the layer the player can attack and recoil off of
    
        private float timeBetweenAttack, timeSinceAttack;
    
        [SerializeField] private float timeBetweenAttack;
        private float timeSinceAttack;

    timeBetweenAttack is the attack cooldown, however it is kept at 0 by default as it has not been set to a value and there is no influence to this value through code.


    Common Issues

    4. ___ does not take damage.

    • Ensure the Player has the “Player” tag assigned to them.
    • Set the Player’s “Attackable Layer” to “Attackable”
    • Ensure the Enemy has the “Attackable” Layer assigned to them.
    Assign the “Player” tag to the Player GameObject.
    Set “Attackable Layer” to “Attackable”.
    Assign the “Attackable” layer to the Enemy GameObject.

    That will be all for Part 3.

    Hopefully this can help you on any issues you may have. However, if you find that your issues weren’t addressed or is a unique circumstance, you can submit a forum post to go into detail on your problem for further assistance.

    #17526
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    6. Player does not recoil when attacking

    Just to add on, there is another problem discovered by @joshkelly. The player’s recoil actually does not work when he attacks. Only the enemy recoils — the player does not, even though he is supposed to as well.

        private void Move()
        {
            if(pState.recoilingX) return;
            rb.velocity = new Vector2(Walkspeed * xAxis, rb.velocity.y);
            anim.SetBool("Walking", rb.velocity.x != 0 && Grounded());
        }

    To fix this, simply add the highlighted section to the Move() function in PlayerController. This prevents the player from being able to move when recoilingX is active, so that the recoil force is not overwritten by our own Move() function.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: