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
- This topic has 4 replies, 2 voices, and was last updated 6 months, 2 weeks ago by Wan Laeta.
-
AuthorPosts
-
June 2, 2024 at 4:35 pm #14897::
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.
June 2, 2024 at 10:43 pm #14903::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 ofGetInputs()
. This relocation ensures that the timer doesn’t reset when you release the Cast/Heal button beforeCastSpell()
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 inGetInputs()
. It being reset is fast enough to cause the if statement inCastSpell()
to receive theGetButtonUp()
function and check thatcastOrHealTimer
is lower than the prerequisite, thus activating and casting the spell.June 3, 2024 at 4:54 pm #14915::I followed you but then another problem occurred. Player does not casts spell after healing but he cannot uses any spells after all.
June 3, 2024 at 5:54 pm #14916::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.
June 3, 2024 at 6:17 pm #14917 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: