Forum begins after the advertisement:


[Part 5] streamlining Cast/heal buttons

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 5] streamlining Cast/heal buttons

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #11923
    Paul Forkus
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    I am having an issue when resetting the cast/heal buttons to the mouse key. After I heal, my sprite automatically fires a fireball if there is enough mana remaining.

    #11924
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi Paul, your code probably looks something like this:

    if(Input.GetMouseButton(0)) {
      // cast heal
    }
    
    if(Input.GetMouseButtonUp(0)) {
      // cast fireball
    }
    

    Add an else in the middle so that if one happens, the other will not.

    if(Input.GetMouseButton(0)) {
      // cast heal
    } else if(Input.GetMouseButtonUp(0)) {
      // cast fireball
    }
    

    Let me know if this works.

    #11929
    Paul Forkus
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Terence,
    I tried the elif statement in my GetInputs function, but I was doing something wrong, because it was still resetting the castOrHealTimer to 0, before it checked the value, and thus casting a fireball after the heal.
    //this is what I tried but couldnt get to work.

    if (Input.GetButton("Cast/Heal"))
         {
             castOrHealTimer += Time.deltaTime;
             Heal();
         }
         else if (Input.GetButtonUp("Cast/Heal"))
         {
             CastSpell();
             castOrHealTimer = 0;
            
         }

    //this is what finally worked for me

     if (Input.GetButtonDown("Cast/Heal"))
     {
         castOrHealTimer = 0;
     }
     if (Input.GetButton("Cast/Heal"))
     {
         castOrHealTimer += Time.deltaTime;
     }
            

    I don’t know if this will cause any issues down the road, but its working better than my last fix, which was to reset the timeToCast variable in the healing function. Thank you for the help!

    #11933
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi Paul, glad you got it to work! You may want to try the following:

     if (Input.GetButton("Cast/Heal"))
     {
         castOrHealTimer += Time.deltaTime;
     }
     else if (Input.GetButtonDown("Cast/Heal"))
     {
         castOrHealTimer = 0;
     }

    I’ve basically swapped the blocks and added an else. The else helps to make sure that if 1 action fires, the other does not fire, and it ensures that any given mouse click only fires 1 single action.

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

Go to Login Page →


Advertisement below: