- Home /
Hold down a GUI button?
I'm using the Ugui and I can't seem to figure out how to do this. I want to make my character rotate whenever I'm holding down a specific button.(I'm using the standard OnClick events to do this) I have a script that works for rotation and it all works, except I have to keep clicking the rotate button to keep rotating. I assume there's an easy way to do this, given that they have an onclick function.
Thanks for your time
Answer by Ali_Jaffer · Oct 07, 2015 at 11:06 AM
Create an Object and add Event Triggers and click " Add New Event Type" and Add Pointerup event and also PointerDown event .
use can use this code for events
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class TouchRace : MonoBehaviour
{
public bool isRacePressed = false;
public bool isbrakePressed = false;
void Start () {
}
void Update () {
if (isRacePressed )
{
//Your code Here
}
else if ( !isRacePressed )
{
//Your Code Here
}
}
public void onPointerDownRaceButton()
{
isRacePressed = true;
}
public void onPointerUpRaceButton()
{
isRacePressed = false;
}
}
[1]: /storage/temp/55697-1.png
does pointerdown and pointerup work with mobile though? they don't have mouse pointers and I read somewhere that it doesn't work with touch input.
The problem I have trying to use this is that the Event Trigger UI doesn't give the the option to use Update functions from my scripts - at least not for me. It only finds custom functions, and those don't make use of the clock-repeat that Update does.
@Ali-Jaffer Does the Event Trigger you set up let you select Update from TouchRace?
Thank you, thank you, thankyou. I've been trying to figure out how to do this for a very long time and you solved all my problems, thank you!
Answer by DiegoSLTS · Oct 06, 2015 at 08:36 PM
The problem is you're using the standard OnClick event. It's called on clicks (press and release in the same object). For other events you need an Event Trigger component, and for the special case of holding down, you'll have to use OnPointerDown and OnPointerUp events. Set a boolean to true when OnPointerDown happens and back to false when OnPointerUp happends. In an Update function rotate on every frame while that bool is set to true.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How can i make a local high score system with C#? 3 Answers
ExecuteEvents.Execute pass object to from ui 1 Answer
Working With GUI 1 Answer