iphone fire button

I would like to make a button that works as a fire button on a mouse for iphone.

gun fires while holding down the button. So far I have only been able to do tap.:expressionless:

Could someone help me in accomplishing this.

Use a RepeatButton instead of button (that being said, on iOS you actually don’t want to use OnGUI at all for this kind of work due to the very negative performance impact)

a repeatButton is a gui button. You said not to use gui.

i am using a texture.

Right, a repeat button is a gui button and I said not to use it. That was in case you use GUI.Button or something similar with GUI (like drawtexture)

if you don’t use autoamtic buttons, just check if the mouse is in the rect of the ā€œbuttonā€ and as long as it is, its pressed down

well, its really basic programming knowledge, ie beginner/entry level.

if (GetMouseButtonDown(1)) //is mouse pressed? this works for touches too don’t worry
{
if (x> mybuttonx x< mybuttonx + mybuttonwidth
{
if y… figure this out the same way
Debug.Log(ā€œfire a message to confirm before going further with the codeā€);
}
}

The above code shows the basic way (it is unfinished, for you to finish) that 2D stuff is done like. 2D stuff has been done like this since the dawn of time, using simple if statements and so on. Going through it we can see if the x position (the Input. mouse x) is past (>) the x position of your virtual button, and its not past the end (less than< mybutton x + its width) then we know its somewhere between over the button.

Doing the same for y, and you have just clicked the button.

Debug.Log is your friend for a lot of things. It can help you step by step confirm if a little bit is working or not. It’s worth getting involved at this level of programming since it does happen an awful lot.

Thanks fo rthe help guys, I know the mouse method. Didnt know it worked for touch though.

it works, but mousebutton is always touch 1. If you want to test it against more than one due to having button + stick for example, you would put this into a loop and instead of the mousebutton just check if the touch you check in this loop run (you would loop with foreach( Touch _touch in Input.touches in C# or for (_touch :Touch in Input.touches on UnityScript) has its _touch.position inside the rect in question.

Also, I wouldn’t do the complicated test there, just use the rects .Contains(_touch.position) and it will tell you if you are inside the given rect :slight_smile: