- Home /
How do i make a continuos touch that triggers more then one "button"?
Hello Unity Community! I am a beginner making my first game on unity, i have searched for a solution and could not find one in the forums, i put my trust in you...
I am trying to stack a few buttons (around 10) vertically one on top of the other to create a sort of a scale for sounds. the problem is i need it to activate more then one button with one continuous touch... in other words: i would like to touch the screen and drag my finger across the 10 buttons, and have them all pressed... currently with a continuos touch, it only recognizes the first button i touched...
i am aware i could have approached this from the wrong angle, so i include my touch_logic script:
using UnityEngine; using System.Collections;
public class TouchButtonLogic : MonoBehaviour {
void Update ()
{
//check for touch
if(Input.touches.Length <= 0)
{
//if no touches detected
}
else //if there is a touch
{
for(int i=0; i
Answer by Owen-Reynolds · Jun 10, 2013 at 09:28 PM
You chopped off your code, but, "slide over counts as click" is easy.
Typical button code keys off of GetMouseUp
or phase=TouchPhase.end
. Simply change to not checking for that stuff. Each frame check MousePosition or Input.Touch[i].position
and press that button. Then have TouchPhase.end
unpress them all.
void Update ()
{
//check for touch
if(Input.touches.Length <= 0)
{
//if no touches detected
}
else //if there is a touch
{
for(int i=0; i<Input.touchCount; i++) //loop through touches on screen
{
if(this.guiTexture.HitTest(Input.GetTouch(i).position))//execute for current touch
{
//if current touch hits our button, run this code
if(Input.GetTouch(i).phase==TouchPhase.Began)
{
print ("the touch has began " + this.name);
audio.Play();
}
if(Input.GetTouch(i).phase==TouchPhase.Ended)
{
print ("the touch has ended " + this.name);
audio.Stop();
}
}
}
}
}
}
i still want the sound to stop when my finger is not on the button, but i dont want to lift it from the screen either... i tried replacing the for loop, but it still did not work as i expected, any ideas?
Easier to do this assu$$anonymous$$g only a single touch. You can add multiples later. Otherwise, same advice.
Don't check for TouchPhase.Began
, since you don't care if they just now tapped the button, or tapped somewhere else and slid onto the button. Likewise don't check for end, since all you care about is the finger was on the button before and now it isn't.
For turning off, you might save the single button that was turned on last frame. If the finger isn't over it this frame, turn off.
Your answer
Follow this Question
Related Questions
Multiple Touch Drag iOS 0 Answers
Tap to Move Drag to Look Null Reference 1 Answer
Touch and drag an object 0 Answers
Click Drag not working on iphone 2 Answers
TouchPhase.Stationary lags on iOS 0 Answers