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?

  • \$\begingroup\$ 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. \$\endgroup\$ – Gnemlock Sep 27 '16 at 3:57

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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