- Home /
Hold Down GUIButton?
I am making my game for mobile and the one problem I have run into is making the button be able to be held down to continually run the script. It worked fine while I was using GetKeyDown to make it the player fly, but now when I press the button on screen it only keeps the script active for less than a second. I was wondering if someone could help me edit this script to make it so the script continually runs while the on screen button is held down. Sorry if this was confusing, English isn't my first language.
public static float jumpDuration = 1.5f;
public float jumpForce = 10.0f;
public void BoostUp () {
jumpDuration = Mathf.MoveTowards (jumpDuration, 0, Time.fixedDeltaTime);
GameObject.Find ("Player").GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpForce));
}
void OnCollisionEnter (Collision Col){
if (Col.gameObject.tag == "Ground") {
jumpDuration = 1.5f;
}
}
Try using Input.Get$$anonymous$$ey as opposed to Input.Get$$anonymous$$eyDown. Get$$anonymous$$eyDown is only executed for a split second when a button is pressed. Get$$anonymous$$ey is executed over and over for as long as the key is pressed down.
That isn't the problem. i need to make it to the on screen button works when you hold it down.
Answer by nu-assets · Aug 13, 2016 at 11:25 AM
Add an
EventTrigger
component to the button object.Add OnPointerDown and OnPointerUp events
Create a script, implement Up and Down handlers and expose a boolean variable
In the Down handler set an exposed variable to
true
In the Up handler set said exposed variable to
false
The exposed var is now reflecting the state of your button
Script
public class Pointer : MonoBehaviour
{
public bool PointerDown
{
get;
private set;
}
public void OnPointerDown()
{
PointerDown = true;
}
public void OnPointerUp()
{
PointerDown = false;
}
}
Inspector