Forum begins after the advertisement:


[Part 7] Article Changes, Common Issues & Bugfixes

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 7] Article Changes, Common Issues & Bugfixes

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #14619
    Joseph Tang
    Moderator

    This is an update post to summarize the changes made to the content of the Metroidvania series.
    Additionally, this post is to compile and go through some common issues that viewers and readers may face when following the Metroidvania series, as well as some solutions that may fix or solve the issues.

    Some solutions given will also be under the assumptions that the code for the mechanic is 1:1 for the relevant part.

    For this part, there are less bugs to be addressed, However, check this post to see if your issue is addressed there.

    In this post, we will go through
    [Part 7]

      *Article Changes*

    • 1. Player respawns at 0 health after leaving game from death. + Moving Player corpse.
    • 2. Bench interaction is inconsistent.


    [Part 7] Death, Respawn, Updating Map and Save system

    Article Changes

    1. Player respawns at 0 health after leaving game from death. + Moving Player corpse.

    [Caused by SavePlayerData() taking place after the death of the player, and not respawning to reset this fact.]

    • Add a code to PlayerController.cs Start() to call the RespawnPlayer() method in GameManager.cs, as well as set the pState.alive to false if the Player’s health is at [0]. This will respawn the player if they are loaded in with 0 health.
    • In the Death() method, set the player’s Rigidbody2D to freeze position, and BoxCollider2D to false. This prevents the dead player from acting as a collider or being influenced by gravity to fall or be pushed by other moving entities to a scene transition.
    • In the Respawned() method, reset the Rigidbody2D constraints and reenable the BoxCollider2D.
        void Start()
        {
            ...
            SaveData.Instance.LoadPlayerData();
            ...
            gravity = rb.gravityScale;
    
            if (Health == 0)
            {
                pState.alive = false;
                GameManager.Instance.RespawnPlayer();
            }
    
            Mana = mana;
            ...
        }
    
        IEnumerator Death()
        {
            ...
            anim.SetTrigger("Death");
            rb.constraints = RigidbodyConstraints2D.FreezePosition;
            GetComponent().enabled = false;
    
            yield return new WaitForSeconds(0.9f);
            ...
        }
    
        public void Respawned()
        {
            if(!pState.alive)
            {
                rb.constraints = RigidbodyConstraints2D.None;
                rb.constraints = RigidbodyConstraints2D.FreezeRotation;
                GetComponent().enabled = true;
                pState.alive = true;
                ...
            }
        }

    2. Bench interaction is inconsistent.

    [Caused by inefficient code, explanation in link below]

    public class Bench : MonoBehaviour
    {
        bool inRange = false;
        public bool interacted;
    
        // Update is called once per frame
        void Update()
        {
            if(Input.GetButtonDown("Interact") && inRange)
            {
                interacted = true;
    
                SaveData.Instance.benchSceneName = SceneManager.GetActiveScene().name;
                SaveData.Instance.benchPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
                SaveData.Instance.SaveBench();
                SaveData.Instance.SavePlayerData();
    
                Debug.Log("benched");
            }
        }
    
        private void OnTriggerStay2D(Collider2D _collision)
        {
            if(_collision.CompareTag("Player") && Input.GetButtonDown("Interact"))
            {
                interacted = true;
            }
        }
    
        void OnTriggerEnter2D(Collider2D _collision)
        {
            if(_collision.CompareTag("Player")) inRange = true;
        }
    
        private void OnTriggerExit2D(Collider2D _collision)
        {
            if (_collision.CompareTag("Player"))
            {
                interacted = false;
                inRange = false;
            }
        }
    }


    That will be all for Part 7.
    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.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: