[4.6 b20] How to make a hold button for the new UI

Hello! This is not a question, but more of an understanding. For a few days i’ve been having problems with the OnPointerDown (it doesn’t show in the event trigger) so here is a simple way to make a hold button which I used to make a character move right while holding down the button. (Yes! it works on mobile too)

First you want to add an event trigger to your button via add component, choose ‘Add new’ and select ‘OnPointerDown’ and hit the ‘+’ sign and add ‘OnPointerUp’. Next put your gameobject (I chose my player which has the tag ‘Player’) into the object box.

Now for the code

Create a C# script and put it into your player character. (I called mine ‘rightmovement’)

using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;
        
    public class rightmovement : MonoBehaviour, IEventSystemHandler {

    public GameObject character;
    public float maxSpeed = 10f;
    bool buttonHeld = false;

    public void pressed (BaseEventData eventData)
	{
	  buttonHeld = true;
	}
    public void notpressed(BaseEventData eventData)
	{
      buttonHeld = false;
    }
    void Start () 
	{
		character = GameObject.FindWithTag ("Player");
	}
    public void Update()
	{
     if (buttonHeld)
      {
	    // Do stuff
	    Debug.Log ("click");
        // Move Character
	    character.rigidbody2D.velocity = Vector3.right * maxSpeed;
	  } 
     else
	   Debug.Log ("not click");
	  }
    }

Now if you click on the box next to your gameobject in the event trigger, you can choose your script and then the button!. make sure your pressed and not pressed voids are PUBLIC.

I am a complete noob to scripting, but this just happened and I thought I’d share! Enjoy! :slight_smile:

In the Event Trigger component you can’t choose the “OnPointerDown” event, the one you want is “PointerDown”, without the “On”.

You can also implement some interfaces for the events you want instead of the more general IEventSystemHandler. There’s IPointerDownHandler and IPointerUpHandler, which force the class to implement OnPointerDown and OnPointerUp functions. This functions are called without any setup in the inspector.

Thanks author, but I think to little bit update this functionality by using boolean properties to set the value for movement variable (in this case for “moveleft”). All other part of work like suggested the author.

using UnityEngine;
using System.Collections;

public class RepeatButton2 : MonoBehaviour {
    // Assign after from editor
    public GameObject player;
    public bool moveleft = false;
    public int speed = 5;

    public void Update ()
    {
	if(moveleft)
        {
            player.transform.Translate(Vector3.left * speed * Time.deltaTime);
        }
	}

    public bool MoveLeft
    {
        get { return moveleft; }
        set { moveleft = value; }
    }
}

@duckduckmoose Thank you soooooo much… i was searching for a result so long… i made my player also move left by copying and pasting that script and changing some things… i made the player move using AddForce on the rigidbody and i made him also jump… so i have 3 buttons… THANK YOU… i wouldnt be able to make this game if i didnt find this answer… a BIG THANKS :smiley:

Thankkks mannnn you the best, worked like a charm