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
- This topic has 0 replies, 1 voice, and was last updated 7 months, 3 weeks ago by Joseph Tang.
-
AuthorPosts
-
May 7, 2024 at 4:48 pm #14551::
This is a supplementary post written for Part 3 of our Metroidvania series, and it aims to address 2 things:
- Missing information in the video, and;
- 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:
- Article Link: https://blog.terresquall.com/2023/05/creating-a-metroidvania-like-hollow-knight-part-3/
- Video Link: https://youtu.be/OepGkSuhqJg
Table of Contents
Missing Information in the Video
- Enemy takes more/twice the damage due to BoxCollider2D
- Enemy continues to move while being recoiled
- Infinite Attack Speed
Common Issues
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.]
- Add a variable on the Enemy.cs to check if damage has been taken.
- In the PlayerController.cs
Hit()
method, check for all enemies damaged and do not apply damage to hit targets again.
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.csEnemyHit()
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.
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.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: