adding an Update function to an Event Trigger

My Goal: a UI button that when pressed and held, pans the camera in one direction

Yes, I’m not much of a coder, but I do my best. I’ve been searching these forums for a solution, and I have found solutions from a couple of years ago. But I don’t quite understand what’s suggested, missing some essential understanding, and the threads are so old I assume no one will answer.

I’m using the “New UI” Event Trigger component on the UI button, and acting to translate a GameObject I call “POV” which includes the camera.

If I include a public void function I call MoveRight in a script, the movement happens just at the press, and doesn’t continue as the button is held. So of course I need something that occurs with each frame, i.e. in an Update function.

The problem is that the Event Trigger UI (under “On Click”) does not allow me to select Update functions. The solutions I found include recurring steps in Update, and tell me to use THAT function.

Where did I go wrong?

Could it be that the default Event Trigger will only accept “On Click” - that is won’t execute until MouseUp? And that I need to create a new custom Event Trigger instead?

Someone please confirm.

Perhaps I have one new Event Trigger for Pointer Down, and in that function just loop a translation, and then a second Event Trigger for Pointer Up, to cancel that loop?

You can implement the IPointerDownHandler, IPointerUpHandler interfaces to figure out when you button gets pressed down and up.
Then you can do your code in the update function while the button is pressed down.
It could look like this:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;

public class DirectionButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    private bool heldDown;

    public void OnPointerDown(PointerEventData eventData)
    {
        heldDown = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        heldDown = false;
    }

    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {

        if (heldDown)
        {
            //Do stuff
        }
    }
}
1 Like

you might need to implement the IPointerExitHandler as well in case the user leaves the button, seem to remember there being threads on the “inconsistency” of the Down/Up handling in those cases.

I’m using exact same code, but when I touch my button the OnPointerDown funciton is called and heldDown is true, and without being called OnPointerUp function, when my Update function is executed heldDown is false. I don’t understand why this is happening. Can you help me? Also, if instead of using Update function, I use FixedUpdate everything is fine, but I really need Update function because I need this to be executed even if Time.scaleTime is 0.