- Home /
Input button
Hello guys. I'm working on a 2d game, i need to create a UI button, for my shooting input
Right now i shoot at " shoot = input.GetButton "Fire1" " "GetButton"to instantly trigger shoot when "Fire1"
i want my Button On pointer down to instantly trigger ("Fire1") when Pointer Is down so i can instant shoot.. .. right now i use this script but it doesn't really work.
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class ShootButton : MonoBehaviour {
public void Shoot () {
Input.GetButton ("Fire1"); // iam tryin to instantly trigger "Fire1" when Shoot is hold down
}
}
i hope you guys can help me out
Answer by Mmmpies · Feb 01, 2015 at 06:28 PM
Input.GetButton("Fire1");
Will check if the Fire1 button has been pressed but you're talking about a UI. Even then you'd need to tell it what to do next, not just reference it.
I'm going to assume you mean the new UI so let us know if that's not the case. Highlight the button on the canvas and look in the inspector. You'll see a bit that says OnClick, for one click that would be what you'd use but for held down you need to add an event trigger. So...
Click Add Component and add an event trigger. Click "Add New Event Type" and select PointerDown from the list.
Now add a script to the button with a public function that does what you want when firing. This is only an example to send out a Debug.Log statement so you can see the button works, no idea how you want to make your game fire.
using Unity.Engine;
using System.Collections;
public class MyFireScript : MonoBehaviour {
public void ImBeingPressed()
{
Debug.Log("BANG!!!!");
}
}
drag that C# script onto your button and then click the + symbol on the eventTrigger you added. A slot appears so drag your button onto that slot.
Now from the dropdown to the right of where select MyFireScript -> ImBeingPressed
and you should get the Debug.Log message in console.
Hello aim getting the Debug.Log message. but the get.input still not work.. it works on the keyboard so it is not the "Fire1" there is wrong, heres what i did
using UnityEngine; using System.Collections;
public class ShootButton : $$anonymous$$onoBehaviour {
public void ImBeingPressed()
{
Debug.Log("BANG!!!!");
Input.GetButton("Fire1");
}
}
what did i do wrong ?
You're pressing a UI button and then only doing the Debug.Log and checking if the keyboard is being pressed. Which makes little sense.
What did your code do before using the UI when you just pressed a button. Input.GetButton only checks for the input being pressed it does NOT make something fire even if the button is called Fire1.
EDIT
In fact in $$anonymous$$onoDevelop (assu$$anonymous$$g you use $$anonymous$$ono) click
Search -> Find in Files
and search for
Input.GetButton("Fire1")
As there must be some script that's doing the actual firing if the keyboard works.
Your answer
Follow this Question
Related Questions
Is there a way to do different things depending on whether the button is being held or tapped? 2 Answers
Buttons in input manager don't do anything 1 Answer
How to use Button as Cross Platform Input 0 Answers
NGUI: Button don't detect input if parent change position on-screen? 1 Answer
First button press don't always register after scene load. 0 Answers