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

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

    This is a supplementary post written for Part 6 of our Metroidvania series, and it aims to address 1 thing:

    1. Missing information in the video.

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


    Table of Contents

    Missing Information in the Video

    1. Bat doesn’t stop chasing and gets recoiled infinitely.
    2. Hit by spikes freezes the game.
    3. 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);} under case EnemyStates.Bat_Chase: before break;. 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 WaitForSeconds WaitForSecondsRealtime(1f);
            ...
            yield return new WaitForSeconds WaitForSecondsRealtime(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() add int 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;
            }
        }

    Edit > Project Settings > Physics2D > Layer Collision Matrix

    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.

    #16196
    Chloe Lim
    Level 9
    Moderator
    Helpful?
    Up
    1
    ::

    Correction: Both the voiceover and Article called static sprite object as sprite shape. Do note that those are 2 different things and we are using a static sprite object in this part of the tutorial

    has upvoted this post.
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: