Forum begins after the advertisement:
[Part 3] Enemy not taking damage
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 3] Enemy not taking damage
- This topic has 8 replies, 2 voices, and was last updated 9 months, 3 weeks ago by Terence.
-
AuthorPosts
-
January 22, 2024 at 9:55 pm #13145::
Hey, I followed the video steps and simply copied the code from the blog, but while recoil works and the player takes damage, the HP from the zombie doesn’t go down when it is hit.
The code used it the one at “Conclusion” in:
Creating a Metroidvania (like Hollow Knight) — Part 3: Melee combat & Enemy AI Base Class
Not sure if anything on the Inspector is set up wrong, I doubled checked on the video and it doesn’t seem to be the case (unless something was changed and not shown enough, since there’s some steps you can barely see where it was clicked before the next session is cut in, like when the Player tag is selected).
January 22, 2024 at 11:55 pm #13146::Hi Allan, let’s look at your
Hit()
function on PlayerController. If it is not triggering, that means that theEnemyHit()
function might not be triggering. Let’s print some text in the Console to check.Add the following lines (highlighted):
void Hit(Transform _attackTransform, Vector2 _attackArea, ref bool _recoilDir, float _recoilStrength) { Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer); if(objectsToHit.Length > 0) { _recoilDir = true; } for(int i = 0; i < objectsToHit.Length; i++) { print("Found " + objectsToHit[i].name); if (objectsToHit[i].GetComponent
() != null) { print("Hitting " + objectsToHit[i].name); objectsToHit[i].GetComponent ().EnemyHit (damage, (transform.position - objectsToHit[i].transform.position).normalized, _recoilStrength); } } } Try to hit the enemy with your player and show me a screenshot of your Console.
January 23, 2024 at 3:35 am #13153::I used the print statement provided, as well as Debug.Log(), but neither showed anything on the console (so there’s little point in showing a screenshot).
The player gets recoil when hitting the enemy, but he also gets lifted on the Y-axis at the same time.
If the enemy touches the player, he gets damaged as intended.When I compile the script I see the warning:
“Assets\Scripts\Zombie.cs(8,10): warning CS0114: ‘Zombie.Start()’ hides inherited member ‘Enemy.Start()’. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.”
I did notice that you used
objectsToHit[i].GetComponent()
but the code on the website includes <Enemy>,
objectsToHit[i].GetComponent<Enemy>().
I tried removing it, but the red squiggles are shown.
January 23, 2024 at 3:50 am #13154::So, I tried adding the following inside Hit() on every possible scope:
print("aaa"); Debug.Log("aaa");
The only time it printed was right at the start, which leads me to think that the if-statements is returning false and the for loop isn’t starting.
So maybe something is wrong with:
Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer);
January 23, 2024 at 4:12 am #13155::lol
I found the error: in Unity I didn’t assign “Attackable” to the Attackable Layer variable!
I watched the video again and it isn’t mentioned =/
At 6:04 it is said to create the layer, then the next time we see the variables on the Inspector from the player object it’s at around 13:00, when you can see that the attackable layer is already selected.January 23, 2024 at 8:29 am #13156::Hi Allan, my bad, it should be
GetComponent<Enemy>()
, notGetComponent()
. The HTML ate my input!Thanks for the heads up on the Attackable layer. I’ve updated the video with a note in the subtitles and a popup.
January 23, 2024 at 8:34 am #13157::By the way, I’ll appreciate if you could let us know when you find more discrepancies / unmentioned features. I’ve gone ahead and added a Bug Reporter badge to your profile as appreciation for your help: https://blog.terresquall.com/community/users/allanvalin/
January 24, 2024 at 3:52 am #13165::lol nice.
When I get the time I’ll start watching the other videos and come back if I find anything.On that topic, the blog post for part 3 has some code changes that aren’t highlighted, but I can’t remember where now.
Btw, some code snippets have the import statements and others don’t, so those are both things you could look into as well (I worked as a proofreader some years ago, so I notice those discrepancies more often than the normal person XD).January 24, 2024 at 10:33 pm #13166 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: