Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a GUI interface with a button for the character's attack, however I want to implement a defense button where the user can hold the button down to block incoming attacks, when the user stops touching the button then the player goes back to idle.

Right now I have the attack button working correctly, whenever the button gets pressed, it triggers a method where the animator gets called and performs the action. I want to implement the getButtonDown("") and getButtonUp("") used commonly with the keyboard input. How can i implement such fucntions to a GUI button?

share|improve this question
    
could you explain how your currently implementing your attack button? There are differant ways, and a good answer would instruct in context of your current implementation. – Gnemlock Sep 27 at 3:57
up vote 0 down vote accepted

Attach the following script to your Block button GameObject. Or adapt into your current script.

The boolean 'isMouseDown' will be true while the mouse is held down. I haven't tried this code exactly, but it should work.

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class MouseEvents : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    private bool isMouseDown = false;

    void Update() {
        // Is the mouse still being held down?
        if (isMouseDown) {
            // Keep blocking
        }
    }

    public void OnPointerDown(PointerEventData data) {
        isMouseDown = true;
        // Start blocking
    }

    public void OnPointerUp(PointerEventData data) {
        isMouseDown = false;
        // Stop blocking
    }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.