Forum begins after the advertisement:


[Part 5] Running + heal bug

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 5] Running + heal bug

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #15070
    Goat
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hello,
    when i take damage, and run(pressing A or D), and press my heal key (C), the characters heals and consumes mana
    All my script is the same, the only thing i didn’t change was the input for cast/heal, i left it had 2 different keys.

    also this fix “The reason for this is due to HitStopTime being called continuously when the player gets hit. Since the player is locked in the dash state, it keeps getting hit and thus freezing time.

    To fix this go to the Enemy base class and add && !Player.Instance.pState.invincible to the if statement in the OnCollisionEnter2D. This stops the HitStopTime from being called once the player is hit and should fix the issue.”

    for dashing in the enemy and staying freezed, has done nothing, and my game freezes. any other possible fix?

    thanks
    Goat

    #15073
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    For the healing bug, take a look at points [2] & [3] from this post:

    [Part 4] Article Changes, Common Issues & Bugfixes

    As for the time stop bug, I will need more information to pinpoint the problem, may you send the Enemy scripts that you are using? Subscripts as well if any. [Example: Zombie, Charger, Bat, Crawler]

    #15074
    Goat
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hello, thanks a lot for the reply.
    I followed the bug fixes in that post, it corrected almost, all the issues, but this one, still figuring out xD.
    I know that the code you have in the end of Part 5, is a bit different from what i have, in PlayerController.cs, so i’m going to check if it changes in the end :D

    Enemy code :

    public class Enemy : MonoBehaviour
    {
    
        [SerializeField] protected float health;
        [SerializeField] protected float recoilLength;
        [SerializeField] protected float recoilFactor;
        [SerializeField] protected bool isRecoiling = false;
    
        [SerializeField] protected PlayerController player;
        [SerializeField] protected float speed;
    
        [SerializeField] protected float damage;
    
        protected float recoilTimer;
        protected Rigidbody2D rb;
    
        // Start is called before the first frame update
        protected virtual void Start()
        {
            rb = GetComponent<Rigidbody2D>();
        }
    
        // Update is called once per frame
        protected virtual void Update()
        {
            if(health <= 0)
            {
                Destroy(gameObject);
            }
            if (isRecoiling)
            {
                if(recoilTimer < recoilLength)
                {
                    recoilTimer += Time.deltaTime;
                }
                else
                {
                    isRecoiling = false;
                    recoilTimer = 0;
                }
            }
        }
    
        public virtual void EnemyHit(float _damageDone, Vector2 _hitDirection, float _hitForce)
        {
            health -= _damageDone;
            if (!isRecoiling)
            {
                rb.AddForce(- _hitForce * recoilFactor * _hitDirection);
                isRecoiling = true;
            }
        }
    
        protected void OnCollisionStay2D(Collision2D _other)
        {
            if (_other.gameObject.CompareTag("Player") && !PlayerController.Instance.pState.invincible)
            {
                Attack();
                PlayerController.Instance.HitStopTime(0, 5, 0.5f);
            }
        }
    
        protected virtual void Attack()
        {
            PlayerController.Instance.TakeDamage(damage);
            Debug.Log("Hit");
        }
    }

    Zombie Code:

    public class Zombie : Enemy
    {
        // Start is called before the first frame update
        protected override void Start()
        {
            base.Start();
            rb.gravityScale = 12f;
        }
    
        // Update is called once per frame
        protected override void Update()
        { 
            base.Update();
            if (!isRecoiling)
            {
                transform.position = Vector2.MoveTowards(transform.position, new Vector2(PlayerController.Instance.transform.position.x, transform.position.y), 
                    speed * Time.deltaTime);
            }
        }
        public override void EnemyHit(float _damageDone, Vector2 _hitDirection, float _hitForce)
        {
            base.EnemyHit(_damageDone, _hitDirection, _hitForce);
        }
    }

    i guess i have this ones like you guys showed :D

    #15075
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Is your RestoreTimeScale() in PlayerController.cs under rhe if return code for pState.dashing in Update(), If it is, move it above the code for you to unfreeze the player. If it’s under the return, then it cannot restore the time scale while the player is dashing

    #15076
    Goat
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hello, thanks again for the reply,
    yes that change worked was the only change i didn’t try :Facepalm:

    About running and healing, it stil occurs, and the fix in the forum, doesn’t suit my problem. if i’m not mistaken, the fix is changing !pState.jumping –> to Grounded().

    private void Update()
        {
            if (pState.cutScene) return;
    
            GetInputs();
            UpdateJumpVariable();
            RestoreTimeScale();
            if (pState.dashing) return;
            FlashWhileInvincible();
            CastSpell();
            Heal();
            if (pState.healing) return;
            Move();
            Flip();
            Jump();
            StartDash();
            Attack();
    
        }

    This is my Move Function-

    private void Move()
        {
            if (pState.healing) rb.velocity = new Vector2(0, 0);
     
                rb.velocity = new Vector2(walkspeed * xAxis, rb.velocity.y);
                anim.SetBool("Walking", rb.velocity.x != 0 && Grounded());
    
        }

    this is my update in Player Controller.
    Everything works fine, if i start healing ,i cant do other actions like move, jump etc.

    but if i’m moving, and (grounded), it assumes, and lets me heal, without the healing animation.
    Just consumes the mana and heals.

    #15077
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Sorry could you send a video and your full PlayerCobtroller.cs for me to see what is happening. Show your playerstate inspector and the mana inspector in the scene as you test the bug.

    #15078
    Goat
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hello, so i found a fix, that i added 3 lines in the Heal Function

    void Heal()
        {
            if(Input.GetButton("Healing") && Health < maxHealth && Mana > 0 && Grounded() && !pState.dashing)
            {
    <strong>            rb.velocity = new Vector2(0, 0);
                anim.SetBool("Walking", false);
                anim.SetBool("Jumping", false);</strong>
                pState.healing = true;
                anim.SetBool("Healing", true);

    now everything is working :D
    thanks

    #15081
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    That’s great to hear that you’ve fixed the issue and that you are able to create a new solution. If you need further assistance for the series, do tell us.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: