- Home /
Continuous shooting by holding iPhone button
I'm trying to create iPhone button that would continuously Instantiate bullets when finger touches this button and is held down, without conflicting with other buttons, for example, rotation joystick. At the moment I have this code that sends only one Message each time when button gets touched ( I also have script from 3rd Person Camera Prefab, which sends message when finger is RELEASED, but that one is much longer and more complicated). Maybe somebody has an idea how to keep sending message to target for few times per second untill finger is released from button...
Thanks in advance!
var recoveryTime = 10;
private var delay = 0; var target : MonoBehaviour; //Target of the message to be sent var message : String ; //Message to be sent
function Update () { if (delay>0){delay -=1;}
if (delay==0){
if(iPhoneInput.touchCount == 1){
var currentTouch:iPhoneTouch = iPhoneInput.touches[0];
if(currentTouch.phase == iPhoneTouchPhase.Began && guiTexture.HitTest(currentTouch.position)){
target.Invoke(message, 0);
delay = recoveryTime;
}
}
}
}
Answer by Bampf · Mar 01, 2011 at 10:01 PM
That code is only testing for TouchPhase.Began. Try allowing the phases Stationary and Moved as well. Or just check for the touch ending:
if ((currentTouch.phase != iPhoneTouchPhase.Ended
&& currentTouch.phase != iPhoneTouchPhase.Cancelled)
&& guiTexture.HitTest(currentTouch.position)
{
target.Invoke(message, 0);
delay = recoveryTime;
}
Arguably you don't need to test the touch phase at all though. You might be able to fire if there's a touch in ANY stage as long as it's on the texture:
if (guiTexture.HitTest(currentTouch.position)
{
target.Invoke(message, 0);
delay = recoveryTime;
}
Phases are mostly used when detecting drags, zooms, or when you need fancy logic like, "cancel selection and start scrolling when they move their finger more than N pixels".
This works good, by changing recovery time I can change fire rate, but there is one problem : when I shoot and want to change direction with rotation joystick, shooting stops when other finger has touched screen in different location outside this guiTexture...Is there some way to fix this?
Yes, fixable. The posted code is using only the first touch in the array: iPhoneInput.touches[0]. Ins$$anonymous$$d you would need to look at all touches in the array, and find the one with the same fingerid as the one that was touching the guitexture last time.
Answer by user-10474 (yahoo) · Mar 19, 2011 at 01:11 AM
Hi, Can you please give me an example on how you managed to implement the joystick and a fire button on this? I am working on a 2d shooter also but can't figure it out the movement (joystick) code :( Thanks
Well, I have a 3rd person camera controller prefab from tornadotwins and with this button I send message to that player script. If you want to know specific details, email me at vmgn021@gmail.com
Your answer
Follow this Question
Related Questions
How can I swipe function that lets the player select buttons? 0 Answers
Wrong Button touch? 1 Answer
iPhone Touch Screen? 2 Answers
Touch GUI texture to toogle animation 0 Answers
help with touch button (android) 1 Answer