Forum begins after the advertisement:
[Part 6 Bug Fixes] Infinite Recoil Bat, Spikes Freeze Game, Optimized Bat Corpse
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 6 Bug Fixes] Infinite Recoil Bat, Spikes Freeze Game, Optimized Bat Corpse
- This topic has 1 reply, 3 voices, and was last updated 3 weeks, 4 days ago by Chloe Lim.
-
AuthorPosts
-
May 10, 2024 at 4:36 pm #14615::
This is a supplementary post written for Part 6 of our Metroidvania series, and it aims to address 1 thing:
- Missing information in the video.
For convenience, below are the links to the article and the video:
- Article Link: https://blog.terresquall.com/2023/07/creating-a-metroidvania-like-hollow-knight-part-6/
- Video Link: https://youtu.be/4J1c5LHk2TE
Table of Contents
Missing Information in the Video
- Bat doesn’t stop chasing and gets recoiled infinitely.
- Hit by spikes freezes the game.
- Bat corpse can be attacked and recoils player.
Missing Information in the Video
1. Bat doesn’t stop chasing and gets recoiled infinitely.
[This is caused by missing code that lets the Bat return to an Idle state from Chase state. Furthermore, there is no velocity change code to change the Bat’s recoil velocity after being recoiled.]
- In Bat.cs script, add
if(_dist > chaseDistance) {ChangeState(EnemyStates.Bat_Idle);}
undercase EnemyStates.Bat_Chase:
beforebreak;
. This should change the Bat to an Idle state if the player exits it’s chaseDistance. - Add a code under
case EnemyStates.Bat_Idle
to reset it’s velocity,rb.velocity = new Vector2(0, 0);
. This will let the Bat stabilize its velocity and stop recoiling without needing to be stopped by a collision.
case EnemyStates.Bat_Idle: rb.velocity = new Vector2(0, 0); if(_dist < chaseDistance) { ChangeState(EnemyStates.Bat_Chase); } break; case EnemyStates.Bat_Chase: rb.MovePosition(Vector2.MoveTowards(transform.position, PlayerController.Instance.transform.position, Time.deltaTime * speed)); FlipBat(); if(_dist > chaseDistance) { ChangeState(EnemyStates.Bat_Idle); } break;
2. Hit by spikes freezes the game.
[This is caused by “IEnumerator RespawnPoint()” in Spikes.cs scaling time to 0 and using “WaitForSeconds”]
- Change all “WaitForSeconds” codes in Spikes.cs to “WaitForSecondsRealTime”. This allows the enumerator to continue as intended while in stopped time without freezing the game.
IEnumerator RespawnPoint() { ... yield return new
WaitForSecondsWaitForSecondsRealtime(1f); ... yield return newWaitForSecondsWaitForSecondsRealtime(UIManager.Instance.sceneFader.fadeTime); ... }
3. Bat corpse can be attacked and recoils player.
[There are different ways to solve this bug but the simplest is to change the Bat’s layer to anything other than “Attackable”.]
- First, create a new layer, and call it “Ignore Player” or something identifiable.
- In Bat.cs, under
ChangeCurrentAnimation()
addint LayerIgnorePlayer = LayerMask.NameToLayer("Ignore Player"); gameObject.layer = LayerIgnorePlayer;
to the If statement for the death state: - Go to Edit > Project Settings > Physics2D > Layer Collision Matrix. Here you can set collision based on Layers.
- Then, turn off collision for both “Default”, “Attackable” & “Ignore Player”. This prevents the corpse from blocking enemies, stacking on each other and player’s movement and attacks.
protected override void ChangeCurrentAnimation() { anim.SetBool("Idle", GetCurrentEnemyState == EnemyStates.Bat_Idle); anim.SetBool("Chase", GetCurrentEnemyState == EnemyStates.Bat_Chase); anim.SetBool("Stunned", GetCurrentEnemyState == EnemyStates.Bat_Stunned); if(GetCurrentEnemyState == EnemyStates.Bat_Death) { anim.SetTrigger("Death"); int LayerIgnorePlayer = LayerMask.NameToLayer("Ignore Player"); gameObject.layer = LayerIgnorePlayer; } }
Note: Depending on your game setup, change the Layers to however you feel is needed.
That will be all for Part 6.
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.October 28, 2024 at 8:30 am #16196 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: