Forum begins after the advertisement:


[Part 5] Player auto casts spell after healing

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 5] Player auto casts spell after healing

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #14897
    Wan Laeta
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    When I change my buttons for “Heal” and “Cast” to the same button, the player always auto casts spell after healing.

    <code>void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
            yAxis = Input.GetAxisRaw("Vertical");
            attack = Input.GetButtonDown("Attack");
    
            if (Input.GetButton("Cast/Heal"))
            {
                castOrHealTimer += Time.deltaTime;
            }
            else
            {
                castOrHealTimer = 0;
            }
        }</code>
    <code>void Heal()
        {
            if (Input.GetButton("Cast/Heal") && castOrHealTimer > 0.05f && Health < maxHealth && Mana > 0 && Grounded() && !pState.dashing)
            {
                pState.healing = true;
                anim.SetBool("Healing", true);
    
                // Healing
                healTimer += Time.deltaTime;
                if (healTimer >= timeToHeal)
                {
                    Health++;
                    healTimer = 0;
                }
    
                // Drain mana
                Mana -= Time.deltaTime * manaDrainSpeed;
            }
            else
            {
                pState.healing = false;
                anim.SetBool("Healing", false);
                healTimer = 0;
            }
        }</code>
    <code>void CastSpell()
        {
            if (Input.GetButtonUp("Cast/Heal") && castOrHealTimer <= 0.05f && timeSinceCast >= timeBetweenCast && Mana >= manaSpellCost)
            {
                Debug.Log("Cast spell!");
                pState.casting = true;
                timeSinceCast = 0;
                StartCoroutine(CastCoroutine());
            }
            else
            {
                // pState.casting = false;
                timeSinceCast += Time.deltaTime;
            }
    
            if (Grounded())
            {
                // Disable downSpell if on the ground
                downSpellFireball.SetActive(false);
            }
    
            // If down spell is active, force player down until grounded
            if (downSpellFireball.activeInHierarchy)
            {
                rb.velocity += downSpellForce * Vector2.down;
            }
        }</code>

    I have no idea how to fix it.

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

    This was discussed in the Part 6 Article:

    In our PlayerController Script, let’s make a change to reset the castOrHealTimer in the CastSpell() method instead of GetInputs(). This relocation ensures that the timer doesn’t reset when you release the Cast/Heal button before CastSpell() is called.

    PlayerController.cs

        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
            yAxis = Input.GetAxisRaw("Vertical");
            attack = Input.GetButtonDown("Attack");
    
            if (Input.GetButton("Cast/Heal"))
            {
                castOrHealTimer += Time.deltaTime;
            }
            else
            {
                castOrHealTimer = 0;
            }
        }
    
        void CastSpell()
        {
            if (Input.GetButtonUp("Cast/Heal") && castOrHealTimer <= 0.05f && timeSinceCast >= timeBetweenCast && Mana >= manaSpellCost)
            {
                pState.casting = true;
                timeSinceCast = 0;
                StartCoroutine(CastCoroutine());
            }
            else
            {
                timeSinceCast += Time.deltaTime;
            }
    
            if (!Input.GetButton("Cast/Heal"))
            {
                castOrHealTimer = 0;
            }
    
            if(Grounded())
            {
                //disable downspell if on the ground
                downSpellFireball.SetActive(false);
            }
            ...
        }

    If this doesn’t work, i can provide a different solution.

    As to why you have this problem, it’s because the castOrHealTimer is being reset everytime you lift up the “Heal/Cast” button in GetInputs(). It being reset is fast enough to cause the if statement in CastSpell() to receive the GetButtonUp() function and check that castOrHealTimer is lower than the prerequisite, thus activating and casting the spell.

    #14915
    Wan Laeta
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    I followed you but then another problem occurred. Player does not casts spell after healing but he cannot uses any spells after all.

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

    I believe your issue is solved by this note in the Part 5 Article,

    Change the if parameter of both Heal() and CastSpell() to a higher float value if needed as 0.05f is a very limited time for a player to tap and fire a spell.

    You can test this by setting the value to 1f.

    #14917
    Wan Laeta
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    I tried changing it to a higher float value and it worked! Thanks for your help!!!!

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

Go to Login Page →


Advertisement below: